2 回答

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊
我認(rèn)為這將是最簡(jiǎn)單的實(shí)現(xiàn):
#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()
還有很多其他方法可以使它變得更復(fù)雜,改進(jìn)您的繪圖以確保您的數(shù)據(jù)類型正確等,但這有望幫助您前進(jìn)。

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊
這樣的事情會(huì)起作用。如果您有任何問題,請(qǐng)告訴我。
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()
添加回答
舉報(bào)