简体   繁体   中英

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'])

在此处输入图片说明

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)

在此处输入图片说明

Thanks! -Abhi

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM