每当我尝试绘制条形图时,我的代码都会不断出现此错误,而我无法解决问题。有人可以帮忙吗?
这是错误消息,我尝试了很多不同的东西来尝试修复它,但没有任何工作。
File "C:\Users\User\Documents\WinPython-64bit-3.5.1.3\python-3.5.1.amd64\lib\site-packages\matplotlib\axes\_axes.py", line 2118, in bar
if h < 0:
TypeError: unorderable types: str() < int()
这是我正在使用的代码
country = []
population = []
per_clean_wat = []
with open('clean_water.csv','r') as cleanwater:
cleanwater = csv.reader(cleanwater)
for row in cleanwater:
c, p, pcw = row
country.append(c)
population.append(p)
per_clean_wat.append(pcw)
c_p = []
for i in range(171):
population[i] = str(float(population[i])).zfill(6)
c_p.append([country[i],population[i]])
c_p = sorted(c_p,key=lambda tup:tup[1], reverse=True)
c_px = []
c_py = []
for i in range(10):
c_px.append(c_p[i][0])
c_py.append(c_p[i][1])
print(c_px)
print(c_py)
plt.margins(0.2)
plt.xticks(list(range(1,len(c_py)+1)),c_px,rotation='vertical',fontsize=12)
plt.ylabel('Population (in millions)')
plt.title('10 most populous countries')
plt.bar(range(1,len(c_py)+1),c_py,align='center',color='green')
当我运行它时,它不会在图表上打印数据。