otmezger
otmezger

Reputation: 10784

python + matplotlib: how to make a bar diagram combining 1 bar and 2 bars?

I'm looking for an option to create a bar diagram that combines 1 bar (per index) and 2 bars.

something like this:

enter image description here

I could fake it making a 2 bar diagram, putting 0 to the first group of bars and then drawing a bar manually... but is there a more elegant way to do it?

Thanks

Upvotes: 2

Views: 275

Answers (1)

Hooked
Hooked

Reputation: 88148

You can adapt as needed, but the key here is that plt.bar takes in a third parameter of the bar width. We can use that and scale the x-coordinate to create the effect you are looking for.

import numpy as np
import pylab as plt

bar_width = 0.3

plt.bar([0,],[3,],2*bar_width,color='k',alpha=.7)

X = np.array([1,2,3])
Y0 = [2,5,7]
Y1 = [5,3,1]

plt.bar(X,Y0,bar_width,color='b',alpha=.7)
plt.bar(X+bar_width,Y1,bar_width,color='r',alpha=.7)

plt.show()

enter image description here

Upvotes: 2

Related Questions