我正在尝试使用matplotlib
创建水平堆叠条形图,但我看不到如何使条形实际堆叠而不是全部在y轴上开始。Matplotlib中的水平堆积条形图
这是我的测试代码。
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
plot_chart(df, fig, ax)
ind = arange(df.shape[0])
ax.barh(ind, df['EndUse_91_1.0'], color='#FFFF00')
ax.barh(ind, df['EndUse_91_nan'], color='#FFFF00')
ax.barh(ind, df['EndUse_80_1.0'], color='#0070C0')
ax.barh(ind, df['EndUse_80_nan'], color='#0070C0')
plt.show()
编辑使用left
kwarg看到tcaswell的评论后。
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
plot_chart(df, fig, ax)
ind = arange(df.shape[0])
ax.barh(ind, df['EndUse_91_1.0'], color='#FFFF00')
lefts = df['EndUse_91_1.0']
ax.barh(ind, df['EndUse_91_nan'], color='#FFFF00', left=lefts)
lefts = lefts + df['EndUse_91_1.0']
ax.barh(ind, df['EndUse_80_1.0'], color='#0070C0', left=lefts)
lefts = lefts + df['EndUse_91_1.0']
ax.barh(ind, df['EndUse_80_nan'], color='#0070C0', left=lefts)
plt.show()
这似乎是正确的做法,但如果是因为它试图添加nan
的值,然后返回nan
特定栏中没有数据失败。
你需要使用'bottom' kwarg – tacaswell
这就给我'TypeError:barh()为关键字参数'bottom''获得了多个值。好像我需要使用'left'。感谢您让我走上正轨。 –
啊,对不起。 – tacaswell