Skip to content Skip to sidebar Skip to footer

How To Set Bar Widths Independent Of Ticks In Matplotlib?

I'm working on a cascade chart (something in this style) using matplotlib. I'd like to get all of my bars of varying widths flush with each other, but I'd like the ticks at the bot

Solution 1:

At the moment the problem is that your index is a regular sequence, so the left hand edges of each bar are positioned at regular intervals. What you want is for index to be a running total of the bar x-values, so that the left hand edge of each bar lines up with the right hand edge of the previous one.

You can do this using np.cumsum():

...
index = np.cumsum(bar_width)
...

Now index will start at bar_width[0], so you'll need to set the left hand edge of the bars to index - bar_width:

rects1 = plt.bar(index-bar_width, ...)

Result:

enter image description here

You'll of course want to play around with the axis limits and label positions to make it look nice.

Post a Comment for "How To Set Bar Widths Independent Of Ticks In Matplotlib?"