'I need guidance on how to move the green bar to the left side of the blue bar in the following bar chart
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()
Solution 1:[1]
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'))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Davide_sd |
