使用具有两列以上的df的子图中的堆积条

问题描述 投票:1回答:2

我的df包含三列月度数据。 Here是档案。 我想使用堆叠条在子图中将每个月的所有三列相互叠加在一起。这是我尝试过的众多代码之一:

df.plot(kind='bar', stacked=True, bottom = df['Yf'])

它创建了一个单独的情节,条形图奇怪地挂起(见下图)。 enter image description here

我想把它放在一个子图中,所有列都堆叠在一起。因此,每个月的最高点是相应月份中三个参数的总和。我还希望能够自由地安排哪个参数应该放在底部,中间和顶部。 我想要这样的东西。 enter image description here在互联网上搜索,还没有解决方案。

python pandas matplotlib histogram stacked-chart
2个回答
1
投票

删除bottom=df['Yf'],因为这告诉plt将酒吧放在df['Yf']的高度。所以就:

df.plot(kind='bar', stacked=True)

您可以选择(bottom, middle, top)订单,如下所示:

orders = ['Yf', 'Ls', 'Lc']
df[orders].plot(kind='bar', stacked=True)

Yf放在底部,然后是LsLc排在最前面。输出:

enter image description here


0
投票

这种解决方法提供了我需要的东西。

ax1.bar(df.index, df['Yf'],  alpha=0.7, color='green')
ax1.bar(df.index, df['Lc'],  bottom=df['Yf'], alpha=0.7, color='red')
ax1.bar(df.index, df['Ls'],  bottom=df['Yf']+df['Lc'], alpha=0.7, color='c')

enter image description here

以为有单行ax1.bar解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.