[英]How to create a stacked bar graph from nested dictionary in python
How can i create stacked bar graph from a dictionary like this 我如何从这样的字典创建堆叠的条形图
{'Chesterville': {'Bachelor': 8, 'Diploma': 5, 'Fail (Promoted)': 5, 'HC': 16},
'Ebony Park': {'Bachelor': 1, 'Diploma': 3, 'Fail (Promoted)': 0, 'HC': 1},
'Makhaza': {'Bachelor': 15, 'Diploma': 9, 'Fail (Promoted)': 13, 'HC': 4}}
You may use pandas to convert the dictionary to a DataFrame and then plot it. 您可以使用pandas将字典转换为DataFrame,然后进行绘制。
import pandas as pd
import matplotlib.pyplot as plt
dic = {'Chesterville': {'Bachelor': 8, 'Diploma': 5, 'Fail (Promoted)': 5, 'HC': 16},
'Ebony Park': {'Bachelor': 1, 'Diploma': 3, 'Fail (Promoted)': 0, 'HC': 1},
'Makhaza': {'Bachelor': 15, 'Diploma': 9, 'Fail (Promoted)': 13, 'HC': 4}}
df = pd.DataFrame(dic)
df.plot(kind="bar", stacked=True)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.