1

The green bar is overlapping the blue bar. Can someone show me how to move the green bar to the right of the blue bar? I want the three bars to align.

import matplotlib.pyplot as plt
import numpy as np
N = 9
labels = ['L', 'S', 'S', 'M', 'W', 'W', 'S', 'R', 'C']    
M = [0.76, 44.91, 28.43, 10.59, 4.06, 6.53, 0.80, 0.02, 0.25]
PO_means = [2.60, 58.19, 17.20, 7.81, 3.10, 7.45, 1.38, 0.06, 1.39]
K_means = [1.3, 48.2, 26, 9.8, 3.2, 7.1, 0.03, 1, 0.7]

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/2, M, width, label='M S and K', color=('#b02a2a'))
rects2 = ax.bar(x + width/2, PO_means, width, label='PO S and K', color=('#055cad'))
rects3 = ax.bar(x + width/2, 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) 
ax.legend(loc='upper right', frameon=False, fontsize=25, markerscale=2)

ax.bar_label(rects1, size = 32, padding=3) 
ax.bar_label(rects2, size = 32,  padding=3) 
ax.bar_label(rects3, size = 32,  padding=3)
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()
Svein
  • 47
  • 3
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). –  May 12 '22 at 18:36

1 Answers1

0

Change these lines:

rects1 = ax.bar(x - width/2, M, width, label='M S and K', color=('#b02a2a'))
rects2 = ax.bar(x + width/2, PO_means, width, label='PO S and K', color=('#055cad'))
rects3 = ax.bar(x + width/2, K_means, width, label='M K', color=('#0b7d53'))

to:

rects1 = ax.bar(x - width, M, width, label='M S and K', color=('#b02a2a'))
rects2 = ax.bar(x, PO_means, width, label='PO S and K', color=('#055cad'))
rects3 = ax.bar(x + width, K_means, width, label='M K', color=('#0b7d53'))
Davide_sd
  • 10,578
  • 3
  • 18
  • 30