Home > Net >  How to convert grouped bar chart from vertical to horizontal in matplotlib
How to convert grouped bar chart from vertical to horizontal in matplotlib

Time:05-30

Can anyone please show me how to change this vertical grouped bar chart to a horizontal grouped bar chart. I need help to alter the code such that the bars are displayed horizontally instead of vertically.

import matplotlib.pyplot as plt
import numpy as np

N = 9
labels = ['L', 'S', 'S', 'M', 'W', 'W', 'S', 'R', 'C']    
M_means = [1, 45, 28, 11, 4, 7, 1, 0.02, 0.3]
PO_means = [3, 58, 17, 8, 3, 8, 1, 0.06, 1]
K = [1, 44, 30, 11, 3, 7, 1, 0.01, 0.5]

x = np.arange(len(labels))  # the label locations
width = 0.30  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width, M_means, width, label='M S and K', color=('#b02a2a'))
rects2 = ax.bar(x, PO_means, width, label='P O S and K', color=('#055cad'))
rects3 = ax.bar(x   width, K_means, width, label='M K', color=('#0b7d53'))

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('% of workday', fontsize=32)
#ax.set_title('Scores by group and gender')
ax.set_xticks(x) 
ax.set_xticklabels(labels, fontsize=32, rotation=15) 
ax.legend(loc='upper right', frameon=False, fontsize=32, markerscale=2)

ax.bar_label(rects1, size = 32, padding=20, rotation=90) 
ax.bar_label(rects2, size = 32,  padding=20, rotation=90) 
ax.bar_label(rects3, size = 32,  padding=20, rotation=90)
plt.xticks(ha='center') 
for tick in ax.xaxis.get_major_ticks():
    tick.label.set_fontsize(32) 
for tick in ax.yaxis.get_major_ticks():
    tick.label.set_fontsize(32) 
plt.ylim(0, 100) 
plt.gca().spines['right'].set_color('none')
plt.gca().spines['top'].set_color('none')
#fig.tight_layout()
plt.show()

CodePudding user response:

Functionally, only two changes are needed:

  1. Change ax.bar to grouped bars converted to hbars

    CodePudding user response:

    Could you also show me how to make a stacked bar chart using the following code?

    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)
    
  • Related