从元组列表构建条形图 - Python
Build a Bar Chart from a List of Tuples - Python
我需要根据我得到的元组列表构建一个条形图,其中键名作为 x 轴上显示的每个条形的标签,值作为条形的高度。
这是我的输入方式:
top20 = [('Blues', 2008), ('Guadeloupe', 1894), ('Yorkshire', 1216), ('Monterrey', 1112), ('Government', 1081), ('Algeria', 972), ('Rotterdam', 920), ('Sardinia', 913), ('Mac OS', 864), ('Coffee', 858), ('Netherlands', 849), ('Oklahoma', 829), ('Tokyo', 817), ('Boating', 801), ('Finland', 765), ('Michigan', 737), ('Tamaulipas', 733), ('Croatia', 722), ('Kagoshima', 701), ('Isuzu', 678)]
这是我目前使用的代码:
plt.bar(range(len(top20)), top20.values(), align='center')
plt.xticks(range(len(top20)), list(top20.keys()))
plt.show()
我知道,逻辑遵循字典作为输入,但我想不出一种方法来完成这项工作。请帮忙,提前谢谢你。
您可以将元组列表转换为列表并使用它:
plt.bar(range(len(top20)), [val[1] for val in top20], align='center')
plt.xticks(range(len(top20)), [val[0] for val in top20])
plt.xticks(rotation=70)
plt.show()
输出:如果删除 align='center'
即:
更新:[OP 在评论中提问]
how would I add value labels to each bar to make the chart more comprehensive?
x_labels = [val[0] for val in top20]
y_labels = [val[1] for val in top20]
plt.figure(figsize=(12, 6))
ax = pd.Series(y_labels).plot(kind='bar')
ax.set_xticklabels(x_labels)
rects = ax.patches
for rect, label in zip(rects, y_labels):
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2, height + 5, label, ha='center', va='bottom')
输出:
还有
dict(top20)
输出:
{'Algeria': 972,
'Blues': 2008,
'Boating': 801,
'Coffee': 858,
'Croatia': 722,
'Finland': 765,
'Government': 1081,
'Guadeloupe': 1894,
'Isuzu': 678,
'Kagoshima': 701,
'Mac OS': 864,
'Michigan': 737,
'Monterrey': 1112,
'Netherlands': 849,
'Oklahoma': 829,
'Rotterdam': 920,
'Sardinia': 913,
'Tamaulipas': 733,
'Tokyo': 817,
'Yorkshire': 1216}
将直接将您的元组列表转换为字典。
我需要根据我得到的元组列表构建一个条形图,其中键名作为 x 轴上显示的每个条形的标签,值作为条形的高度。 这是我的输入方式:
top20 = [('Blues', 2008), ('Guadeloupe', 1894), ('Yorkshire', 1216), ('Monterrey', 1112), ('Government', 1081), ('Algeria', 972), ('Rotterdam', 920), ('Sardinia', 913), ('Mac OS', 864), ('Coffee', 858), ('Netherlands', 849), ('Oklahoma', 829), ('Tokyo', 817), ('Boating', 801), ('Finland', 765), ('Michigan', 737), ('Tamaulipas', 733), ('Croatia', 722), ('Kagoshima', 701), ('Isuzu', 678)]
这是我目前使用的代码:
plt.bar(range(len(top20)), top20.values(), align='center')
plt.xticks(range(len(top20)), list(top20.keys()))
plt.show()
我知道,逻辑遵循字典作为输入,但我想不出一种方法来完成这项工作。请帮忙,提前谢谢你。
您可以将元组列表转换为列表并使用它:
plt.bar(range(len(top20)), [val[1] for val in top20], align='center')
plt.xticks(range(len(top20)), [val[0] for val in top20])
plt.xticks(rotation=70)
plt.show()
输出:如果删除 align='center'
即:
更新:[OP 在评论中提问]
how would I add value labels to each bar to make the chart more comprehensive?
x_labels = [val[0] for val in top20]
y_labels = [val[1] for val in top20]
plt.figure(figsize=(12, 6))
ax = pd.Series(y_labels).plot(kind='bar')
ax.set_xticklabels(x_labels)
rects = ax.patches
for rect, label in zip(rects, y_labels):
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2, height + 5, label, ha='center', va='bottom')
输出:
还有
dict(top20)
输出:
{'Algeria': 972,
'Blues': 2008,
'Boating': 801,
'Coffee': 858,
'Croatia': 722,
'Finland': 765,
'Government': 1081,
'Guadeloupe': 1894,
'Isuzu': 678,
'Kagoshima': 701,
'Mac OS': 864,
'Michigan': 737,
'Monterrey': 1112,
'Netherlands': 849,
'Oklahoma': 829,
'Rotterdam': 920,
'Sardinia': 913,
'Tamaulipas': 733,
'Tokyo': 817,
'Yorkshire': 1216}
将直接将您的元组列表转换为字典。