matplotlib并排显示两个图表,第三个图表覆盖第二个图表

时间:2016-03-22 05:00:26

标签: pandas matplotlib jupyter-notebook

我正在尝试使用matplotlib(更具体地说是来自pandas的绘图方法)在ipython笔记本中并排绘制两个图表,第三个图表覆盖第二个图表并使用辅助y轴。但是,我无法让叠加层工作。

目前这是我的代码:

import matplotlib.pyplot as plt
%matplotlib inline

fig, axs = plt.subplots(1,2)
fig.set_size_inches(12, 4)

top10.plot(kind='barh', ax=axs[0])
top10_time_trend.T.plot(kind='bar', stacked=True, legend=False, ax=axs[1])
time_trend.plot(kind='line', ax=axs[1], ylim=0, secondary_y=True)

我得到了我正在寻找的并排结构,但只有第一个(top10)和last(time_trend)图是可见的。我的输出如下:

enter image description here

当单独绘制时,未示出的绘图(top10_time_trend)看起来像这样

enter image description here

我想要完成的是看起来像这样的东西,即折叠堆积条形图的折线图。

enter image description here

1 个答案:

答案 0 :(得分:1)

执行此操作的最佳方法是创建第三个轴:

ax3 = ax[1].twinx()

然后

top10_time_trend.T.plot(kind='bar', stacked=True, legend=False, ax=ax3)

如果这对你有用,请告诉我。

在这里,您可以找到matplotlib docs http://matplotlib.org/examples/api/two_scales.html中使用twinx()的示例