14

我想从数据 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))

谢谢。

4

2 回答 2

11

在当前版本的 Pandas 中,为此目的DataFrame.plot提供了关键字。layout

df.plot(subplots=True, layout=(2,2), ...)
于 2015-05-02T07:21:27.757 回答
9

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 上添加了一些示例,以显示如何进一步配置它。

于 2013-06-14T10:13:23.807 回答