Abhi
Abhi

Reputation: 6255

formatting x-axis labels with two y-axis in matplotlib (bar and line plot)

I am plotting a bar and line plot in one figure and having problems with correctly formatting the shared x-axis tick labels. The point on the line is not in sync with the center of the bar where the tick label is drawn.

PS: I am plotting through pandas plot function

Example:

A. Single Bar plot (works fine)

libs_summary_pandas_df[['read_count']].plot(kind='bar',ax=axis,color=['#E41A1C'])

enter image description here

B. Overlaying with line plot on the secondary y-axis (x-axis labels are messed up)

libs_summary_pandas_df.total_yield.map(lambda x: x/1000000000.0).plot(kind='line',ax=axis)

enter image description here

Thanks! -Abhi

Upvotes: 2

Views: 1227

Answers (1)

Guillaume
Guillaume

Reputation: 251

As stated by the matplotlib's bar documentation which is used internally py pandas.plot :

align ‘edge’ (default) | ‘center’

For vertical bars, align = ‘edge’ aligns bars by their left edges in left, while align = ‘center’ interprets these values as the x coordinates of the bar centers.

So adding try adding the keyword align ='center' to you first plot call and that might get aligned your x-axis.

Upvotes: 2

Related Questions