简体   繁体   English

如何合并由两种方法(df.plot 和 matplotlib.pyplot)生成的图

[英]How to merge plots generated by two methods (df.plot and matplotlib.pyplot)

I want to plot two charts (top-bottom) in one figure, using matplotlib.pyplot and dataframe.plot, respectively.我想分别使用 matplotlib.pyplot 和 dataframe.plot 在一个图中绘制两个图表(上下)。 But the below code generates two figures, with an empty bottom plot in the first figure.但是下面的代码会生成两个图形,第一个图中的底部图是空的。 I am not sure how to merge them into one figure and would highly appreciate some help.我不确定如何将它们合并到一个图中,非常感谢一些帮助。

fig, [ax1, ax2] = plt.subplots(2,1,sharex = True)
ax1.plot(info['Yr'], info['mean'], label = 'mean')
ax1.plot(info['Yr'], info['CI-2.5%'], label = 'CI-2.5%')
ax1.plot(info['Yr'], info['CI-97.5%'], label = 'CI-97.5%')

ax2 = df.set_index('Yr').plot.bar(stacked=True)

You can supply the ax argument to the plot.bar command with the ax you created and it'll use it, effectively "merging" your two plots.您可以使用您创建的 ax 将ax参数提供给plot.bar命令,它会使用它,有效地“合并”您的两个绘图。

fig, [ax1, ax2] = plt.subplots(2,1,sharex = True)
ax1.plot(info['Yr'], info['mean'], label = 'mean')
ax1.plot(info['Yr'], info['CI-2.5%'], label = 'CI-2.5%')
ax1.plot(info['Yr'], info['CI-97.5%'], label = 'CI-97.5%')

ax2 = df.set_index('Yr').plot.bar(stacked=True, ax = ax2)

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.bar.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.bar.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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