问题 python pandas列和行中的DataFrame子图


我想从数据4列DataFrame生成一个子图,分为2行和2列

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

但是下面将给出4行和1列图

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

谢谢。


8768
2018-02-15 04:54


起源

你应该手工完成, import matplotlib.pyplot as plt,然后像 for i in df: plt.subplot(2,2,i+1);plt.plot(df[i]); - Thorsten Kranz
@ tesla1060我想大熊猫可以允许某种形式 figshape 论据... - Phillip Cloud


答案:


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()

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


9
2018-06-14 10:13





在当前版本的熊猫中, DataFrame.plot 特色 layout 用于此目的的关键字。

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

7
2018-05-02 07:21