-1

Could someone please show me how to change this horizontal bar chart to a stacked bar chart? I need all the values to be displayed inside the respective bar.

import matplotlib.pyplot as plt
import numpy as np

N = 9
labels = ('Les', 'Satr', 'Suvra', 'Mers', 'Wonot', 'Wlkers', 'Svesert', 'Ratr', 'Cxyrte')
Matr_means = [1, 45, 28, 11, 4, 7, 1, 0.02, 0.3]
Pbtrlio_means = [3, 58, 17, 8, 3, 8, 1, 0.06, 1]
Kzarqyt_means = [1, 44, 30, 11, 3, 7, 1, 0.01, 0.5]

# rename x/width to y/height
y = np.arange(len(labels))
height = 0.30

fig, ax = plt.subplots()

# use ax.barh instead of ax.bar
rects1 = ax.barh(y - height, Matr_means, height, label='Matrin', color='#b02a2a')
rects2 = ax.barh(y, Pbtrlio_means, height, label='Pokoloi', color='#055cad')
rects3 = ax.barh(y + height, Kzarqyt_means, height, label='Marikgt', color='#0b7d53')

# swap set_x* methods with set_y* methods
plt.xticks(ha='center', fontsize=20) 
ax.set_xlabel('% of workday', fontsize=20)
ax.set_yticks(y)
ax.set_yticklabels(labels, fontsize=20)
ax.legend(loc='upper right', frameon=False, fontsize=20, markerscale=2)
plt.xlim(0, 100) 
plt.gca().spines['right'].set_color('none')
plt.gca().spines['left'].set_color('none')
plt.gca().spines['top'].set_color('none')

ax.bar_label(rects1, size = 20, padding=10)
ax.bar_label(rects2, size = 20, padding=10)
ax.bar_label(rects3, size = 20, padding=10)
Trenton McKinney
  • 43,885
  • 25
  • 111
  • 113
Svein
  • 37
  • 2
  • Does this answer your question? [How to convert grouped bar chart from vertical to horizontal](https://stackoverflow.com/questions/72359816/how-to-convert-grouped-bar-chart-from-vertical-to-horizontal) – Trenton McKinney May 29 '22 at 18:35
  • Also [stacked bar plot using matplotlib](https://stackoverflow.com/q/44309507/7758804) and [Horizontal stacked bar chart in Matplotlib](https://stackoverflow.com/q/16653815/7758804) – Trenton McKinney May 29 '22 at 18:36

0 Answers0