python plt.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 个回答
2
这是我用来搞明白这个问题的链接:这里
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()