如何将单独的 Pandas DataFrames 绘制为子图? [英] How can I plot separate Pandas DataFrames as subplots?
问题描述
我有几个 Pandas DataFrames 共享相同的值比例,但具有不同的列和索引.调用 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=True
给 plt.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
.
这篇关于如何将单独的 Pandas DataFrames 绘制为子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!