這是我計(jì)劃用于創(chuàng)建條形圖的代碼。忽略下一行。我編寫(xiě)此代碼只是為了平衡代碼和細(xì)節(jié)。import numpy as npimport pandas as pdimport matplotlib.pyplot as pltdef bar1(): df=pd.read_csv('C:\\Users\Bhuwan Bhatt\Desktop\IP PROJECT\Book1.csv',encoding= 'unicode_escape') x=np.arange(11) Countries=df['Country'] STotalMed=df['SummerTotal'] WTotalMed=df['WinterTotal'] plt.bar(x-0.25,STotalMed,width=.2, label='Total Medals by Countries in Summer',color='g') plt.bar(x+0.25,WTotalMed,width=.2, label='Total Medals by Countries in Winter',color='r') plt.xticks(np.arange(11),Countries,rotation=30) plt.title('Olympics Data Analysis of Top 10 Countries',color='red',fontsize=10) plt.xlabel('Countries') plt.ylabel('Total Medals') plt.grid() plt.legend() plt.show()bar1()由于某種原因我收到此錯(cuò)誤:Traceback (most recent call last): File "C:/Users/Bhuwan Bhatt/Desktop/dsd.py", line 19, in <module> bar1() File "C:/Users/Bhuwan Bhatt/Desktop/dsd.py", line 10, in bar1 plt.bar(x-0.25,STotalMed,width=.2, label='Total Medals by Countries in Summer',color='g') File "C:\Users\Bhuwan Bhatt\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\pyplot.py", line 2471, in bar return gca().bar( File "C:\Users\Bhuwan Bhatt\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\__init__.py", line 1438, in inner return func(ax, *map(sanitize_sequence, args), **kwargs)INFO-----> SummerTimesPart : 各個(gè)國(guó)家在夏季參加的次數(shù) WinterTimesPart : 各個(gè)國(guó)家在冬季參加的次數(shù)
1 回答

森林海
TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊
只是改變
x=np.arange(11) to x = np.arange(len(df))
和
plt.xticks(np.arange(11),Countries,rotation=30) to plt.xticks(x,Countries,rotation=30)
添加回答
舉報(bào)
0/150
提交
取消