Horizontal stacked bar chart in Matplotlib
Here's a solution, although I'm sure there must be a better way of doing it. The series.fillna(0)
part replaces any nan
with 0.
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'].fillna(0)
ax.barh(ind, df['EndUse_91_nan'], color='#FFFF00', left=lefts)
lefts = lefts + df['EndUse_91_1.0'].fillna(0)
ax.barh(ind, df['EndUse_80_1.0'], color='#0070C0', left=lefts)
lefts = lefts + df['EndUse_91_1.0'].fillna(0)
ax.barh(ind, df['EndUse_80_nan'], color='#0070C0', left=lefts)
plt.show()
Since you are using pandas, it's worth mentioning that you can do stacked bar plots natively:
df2.plot(kind='bar', stacked=True)
See the visualisation section of the docs.