python plt.bar连接错误

时间:2014-05-08 14:33:00

标签: python matplotlib concatenation

我有2个列表,我想把它们放在一个pyplot.bar

sorted_ratings2 = ['8,3','8,2','8,2','8,3','8,5','8,4','8,2','8,5', '8,2','8,2']

Year = ['1921','1925','1926','1927','1931','1931','1934','1936','1939','1939','1939']



plt.bar((Year), (sorted_ratings2)
plt.suptitle('Ratings based on years', fontsize=14)
plt.ylabel('Rating', fontsize=12)
plt.xlabel('Year', fontsize=12)  
plt.show()


Output: cannot concatenate 'str' and 'float' objects

我尝试使用plt.bar进行绘图但得到此错误。我做错了什么?

1 个答案:

答案 0 :(得分:2)

Here是我用来弄清楚的链接

from matplotlib import pylab as plt
import numpy as np
sorted_ratings2 = [8.3, 8.2, 8.2, 8.3, 8.5, 8.4, 8.2, 8.5, 8.2, 8.2]

Years = ['1921', '1925', '1926', '1927', '1931', '1931', '1934', '1936', '1939', '1939']

x_pos = np.arange(len(Years))


plt.bar(x_pos, sorted_ratings2)
plt.suptitle('Ratings based on years', fontsize=14)
plt.xticks(x_pos, Years)
plt.ylabel('Rating', fontsize=12)
plt.xlabel('Year', fontsize=12)  
plt.show()

enter image description here