e.Fro
e.Fro

Reputation: 397

How to create vertical lines between x-values on barplot

I am currently working on a barplot which looks like this: The current version In order to provide more clarity, I would like to add a vertical line that should point out the separation between the x values. I've drawn an example here: the desired version

In order to draw the diagram, I am using the plot function from pandas on the corresponding dataframe:

produced_items.plot(kind='bar', legend=True,title="Produced Items - Optimal solution",xlabel="Months",ylabel='Amount',rot=1,figsize=(15,5),width=0.8)

I hoped to find a parameter in matplotlib, that yields the desired behavior, but I didn't find anything, that could help. Another solution that comes in my mind is to plot a vertical line between each x-value but i wonder if there is a built-in way to accomplish this?

Upvotes: 0

Views: 571

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

Let's try modifying the minor ticks:

from matplotlib.ticker import MultipleLocator

ax = df.plot.bar()

# set the minor ticks
ax.xaxis.set_minor_locator(MultipleLocator(0.5))

# play with the parameters to get your desired output
ax.tick_params(which='minor', length=15, direction='in')

Output:

enter image description here

Upvotes: 1

Related Questions