2 回答
TA贡献1806条经验 获得超8个赞
我认为这将是最简单的实现:
#import libraries
import pandas as pd
import matplotlib.pyplot as plt
#read your txt file which is formatted as a csv into a dataframe and name your cols
df = pd.read_csv('my_file.txt',names=['name','number'])
print(df.head())
#plot it
plt.bar(df.name,df.number) #this is equivalent to df['name'],df['number']
plt.show()
还有很多其他方法可以使它变得更复杂,改进您的绘图以确保您的数据类型正确等,但这有望帮助您前进。
TA贡献2021条经验 获得超8个赞
这样的事情会起作用。如果您有任何问题,请告诉我。
import matplotlib.pyplot as plt
filepath = r"C:\Users*me*\Desktop\my_file.txt"
with open(filepath) as file:
entries = [x.split(",") for x in file.readlines()] # Read the text, splitting on comma.
entries = [(x[0],int(x[1])) for x in entries] # Turn the numbers into ints.
entries.sort(key=lambda x:x[1], reverse=True) # Sort by y-values.
x_coords = [x[0] for x in entries]
y_coords = [x[1] for x in entries]
plt.bar(x_coords,y_coords) # Draw a bar chart
plt.show()
添加回答
举报