Below code:
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
p1 = [20, 35, 30, 35, 7]
p2 = [25, 32, 34, 20, 55]
p3 = [21, 361, 341, 205,151]
width = 0.35 # the width of the bars: can also be len(x) sequence
fig, ax = plt.subplots()
fig.set_figheight(7)
fig.set_figwidth(13)
ax.bar(labels, p1, width, label='p1')
ax.bar(labels, p2, width, bottom=p1, label='p2')
ax.bar(labels, p3, width, bottom=p2, label='p3')
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.legend()
figure(num=None, figsize=(14, 7), dpi=80, facecolor='w', edgecolor='k')
plt.show()
renders:
I'm attempting to convert this chart to horizontal - each stacked bar is displayed horizontally.
I change the code to use hbar
:
ax.hbar(labels, p1, width, label='p1')
ax.hbar(labels, p2, width, bottom=p1, label='p2')
ax.hbar(labels, p3, width, bottom=p2, label='p3')
But this causes the error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-39-71e7a37d2257> in <module>
10 fig.set_figwidth(13)
11
---> 12 ax.hbar(labels, p1, width, label='p1')
13 ax.hbar(labels, p2, width, bottom=p1, label='p2')
14 ax.hbar(labels, p3, width, bottom=p2, label='p3')
AttributeError: 'AxesSubplot' object has no attribute 'hbar'
How to amend the chart so that the stacked bar chart. can be displayed horizontally ?