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()
添加回答
舉報