Maria
Maria

Reputation: 115

Separate two groups of bars in matplotlib

Do you know if it is possible to separate the bars into two groups of different sizes, but maintaining both in the same plot? I have this code:

import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt

objects = ('A', 'B', 'C', 'D', 'E', 'F', 'G')
y_pos = np.arange(len(objects))
performance = [15.3, 25.8, 37.1, 50.0, 15.0, 18.5, 28.9]
plt.bar(y_pos, performance, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel('Reduction Error')
plt.title("")

plt.show()

And I would like to have A and B close together, then some space, and then all the other bars. I found this issue Function to create grouped bar plot, but I would like to keep each name under the bar and not group them as in the example. Thank you for your help!

Upvotes: 1

Views: 2063

Answers (1)

Joe
Joe

Reputation: 12417

If I understood you correctly, you can do in this way:

objects = ('A', 'B', 'C', 'D', 'E', 'F', 'G')
x = [1,1.8,5,6,7,8,9]
performance = [15.3, 25.8, 37.1, 50.0, 15.0, 18.5, 28.9]

plt.bar(x, performance, align='center', alpha=0.5)
plt.xticks(x, objects)
plt.ylabel('Reduction Error')
plt.title("")
plt.show()

enter image description here

Or use 2 instead of 1.8 in x to have some space between A and B

Upvotes: 1

Related Questions