[英]How can I create a stacked bar chart in matplotlib where the stacks vary from bar to bar?
So I have a pandas DataFrame that looks something like this:所以我有一个 pandas DataFrame 看起来像这样:
year country total
0 2010 USA 10
1 2010 CHIN 12
2 2011 USA 8
3 2011 JAPN 12
4 2012 KORR 7
5 2012 USA 10
6 2013 CHIN 9
7 2013 USA 13
I'd like to create a stacked bar chart in matplotlib, where there is one bar for each year and stacks for the two countries in that year with height based on the total
column.我想在 matplotlib 中创建一个堆积条形图,其中每年有一个条形图,该年有两个国家的堆叠,高度基于total
列。 The color should be based on the country and be represented in the legend.颜色应基于国家并在图例中表示。
I can't seem to figure out how to make this happen.我似乎无法弄清楚如何做到这一点。 I think I could do it using for loops to go through each year and each country, then construct the bar with the color corresponding to values in a dictionary.我想我可以通过每年和每个国家使用到 go 的 for 循环来做到这一点,然后使用与字典中的值相对应的颜色构建条形图。 However, this will create individual legend entries for each individual bar such that there are 8 total values in the legend.但是,这将为每个单独的条创建单独的图例条目,以便图例中有 8 个总值。 This is also a horribly inefficient way to graph in matplotlib as far as I can tell.据我所知,这也是在 matplotlib 中绘制图表的一种非常低效的方法。
Can anyone give some pointers?任何人都可以提供一些指示吗?
You need to transform your df
first.您需要先转换df
。 It can be done via the below:可以通过以下方式完成:
df = pd.DataFrame({'year': {0: 2010, 1: 2010, 2: 2011, 3: 2011, 4: 2012, 5: 2012, 6: 2013, 7: 2013},
'country': {0: 'USA', 1: 'CHIN', 2: 'USA', 3: 'JAPN', 4: 'KORR', 5: 'USA', 6: 'CHIN', 7: 'USA'},
'total': {0: 10, 1: 12, 2: 8, 3: 12, 4: 7, 5: 10, 6: 9, 7: 13}})
df2 = df.groupby(['year',"country"])['total'].sum().unstack("country")
print (df2)
#
country CHIN JAPN KORR USA
year
2010 12.0 NaN NaN 10.0
2011 NaN 12.0 NaN 8.0
2012 NaN NaN 7.0 10.0
2013 9.0 NaN NaN 13.0
#
ax = df2.plot(kind='bar', stacked=True)
plt.show()
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.