在matplotlib中用两个y轴格式化x轴标签(条形图和折线图) [英] formatting x-axis labels with two y-axis in matplotlib (bar and line plot)
问题描述
我正在一个图中绘制条形图和折线图,但在正确格式化共享 x 轴刻度标签时遇到问题.线上的点与绘制刻度标签的条形中心不同步.
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:我正在通过熊猫绘图功能进行绘图
示例:
A.单条形图(工作正常)
A. Single Bar plot (works fine)
libs_summary_pandas_df[['read_count']].plot(kind='bar',ax=axis,color=['#E41A1C'])
B.在辅助 y 轴上叠加线图(x 轴标签混乱)
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)
谢谢!-阿比
推荐答案
如 matplotlib 的栏文档所述 内部使用 py pandas.plot :
As stated by the matplotlib's bar documentation which is used internally py pandas.plot :
对齐‘边缘’(默认)|‘中心’
对于垂直条,align ='edge'通过其左边缘在左侧对齐条,而align ='center'将这些值解释为条中心的x坐标.
因此,请尝试在您的第一次绘图调用中添加关键字align ='center',这样可能会使您的x轴对齐.
So adding try adding the keyword align ='center' to you first plot call and that might get aligned your x-axis.
这篇关于在matplotlib中用两个y轴格式化x轴标签(条形图和折线图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!