如何防止在Python中截断双轴条形图?

时间:2020-04-03 20:03:46

标签: python pandas matplotlib plot

我正在尝试创建双轴线和条形图,但是末端条被剪裁了。我想在不更改x限制的情况下避免这种情况的发生。 这就是我要重新创建的: Plot

折线图应在条形图的前面。

我使用的代码如下:-

pivot_df.index=pivot_df.index.strftime("%Y-%m-%d")
fig, ax = plt.subplots()
pivot_df.loc[:,['A','B','C']].plot.bar(stacked=True, color=colors,ax=ax)#Bar Plot
ax2 = ax.twinx()
pivot_df.loc[:,['Contribution_B', 'Contribution_C']].plot(ax=ax2,color=colors2,marker='o') #Line Plot
ax2.set_ylim(0,100)

1 个答案:

答案 0 :(得分:0)

看起来第二幅图的调用正在更改x轴的极限。也许尝试在第二个图之前获得限制,然后在第二个图之后恢复它们

pivot_df.index=pivot_df.index.strftime("%Y-%m-%d")
fig, ax = plt.subplots()
pivot_df.loc[:,['A','B','C']].plot.bar(stacked=True, color=colors,ax=ax)#Bar Plot
xlims = ax.get_xlim()
ax2 = ax.twinx()
pivot_df.loc[:,['Contribution_B', 'Contribution_C']].plot(ax=ax2,color=colors2,marker='o') #Line Plot
ax2.set_ylim(0,100)
ax2.set_xlim(xlims)