列和行中的python pandas DataFrame子图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14888473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:38:52  来源:igfitidea点击:

python pandas DataFrame subplot in columns and rows

pythondataframepandassubplot

提问by tesla1060

I would like to produce a subplot from data 4 column DataFrame into 2 rows and 2 columns

我想将数据 4 列 DataFrame 中的子图生成为 2 行和 2 列

df =pd.DataFrame(np.random.randn(6,4),index=pd.date_range('1/1/2000',periods=6, freq='1h'))

However below will give a 4 row and 1 column plot

然而,下面将给出一个 4 行和 1 列的图

 df.plot(use_index=False, title=f, subplots=True, sharey=True, figsize=(8, 6))

Thanks.

谢谢。

回答by Joop

cplcloud's answer works, but following code will give you a bit more structure so that you can start configuring more if you do not need the loop.

cplcloud 的答案有效,但以下代码将为您提供更多结构,以便您在不需要循环时可以开始配置更多。

fig, axes = plt.subplots(nrows=2, ncols=2)
fig.set_figheight(6)
fig.set_figwidth(8)
df[0].plot(ax=axes[0,0], style='r', label='Series'); axes[0,0].set_title(0)
df[1].plot(ax=axes[0,1]); axes[0,1].set_title(1)
df[2].plot(ax=axes[1,0]); axes[1,0].set_title(2)
df[3].plot(ax=axes[1,1]); axes[1,1].set_title(3)
fig.tight_layout()

Added some example on axis 0 to show how you can further configure it.

在轴 0 上添加了一些示例以展示如何进一步配置它。

回答by David Z

In current versions of Pandas, DataFrame.plotfeatures the layoutkeyword for this purpose.

在当前版本的 Pandas 中,DataFrame.plot具有layout用于此目的的关键字。

df.plot(subplots=True, layout=(2,2), ...)