tes*_*060 12 python dataframe pandas subplot
我想从数据4列DataFrame生成一个子图,分为2行和2列
df =pd.DataFrame(np.random.randn(6,4),index=pd.date_range('1/1/2000',periods=6, freq='1h'))
Run Code Online (Sandbox Code Playgroud)
但是下面将给出4行和1列图
df.plot(use_index=False, title=f, subplots=True, sharey=True, figsize=(8, 6))
Run Code Online (Sandbox Code Playgroud)
谢谢.
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()
Run Code Online (Sandbox Code Playgroud)
在轴0上添加了一些示例,以说明如何进一步配置它.
在当前版本的Pandas中,为此目的DataFrame.plot提供layout关键字.
df.plot(subplots=True, layout=(2,2), ...)
Run Code Online (Sandbox Code Playgroud)