如何将单独的Pan​​das DataFrame绘制为子图? [英] How can I plot separate Pandas DataFrames as subplots?

查看:158
本文介绍了如何将单独的Pan​​das DataFrame绘制为子图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个Pandas DataFrame共享相同的价值尺度,但是具有不同的列和索引.调用df.plot()时,会得到单独的绘图图像.我真正想要的是将它们与子图放置在同一块图上,但是不幸的是,我未能提出解决方案,并且希望获得一些帮助.

I have a few Pandas DataFrames sharing the same value scale, but having different columns and indices. When invoking df.plot(), I get separate plot images. what I really want is to have them all in the same plot as subplots, but I'm unfortunately failing to come up with a solution to how and would highly appreciate some help.

推荐答案

您可以使用matplotlib手动创建子图,然后使用ax关键字在特定子图上绘制数据框.例如4个子图(2x2):

You can manually create the subplots with matplotlib, and then plot the dataframes on a specific subplot using the ax keyword. For example for 4 subplots (2x2):

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=2, ncols=2)

df1.plot(ax=axes[0,0])
df2.plot(ax=axes[0,1])
...

此处axes是一个包含不同子图轴的数组,您可以通过索引axes来访问其中一个.
如果要共享x轴,则可以提供sharex=Trueplt.subplots.

Here axes is an array which holds the different subplot axes, and you can access one just by indexing axes.
If you want a shared x-axis, then you can provide sharex=True to plt.subplots.

这篇关于如何将单独的Pan​​das DataFrame绘制为子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆