[英]python pandas DataFrame subplot in columns and rows
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. 谢谢。
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上添加了一些示例,以说明如何进一步配置它。
在当前版本的Pandas中, DataFrame.plot
为此目的提供了layout
关键字。
df.plot(subplots=True, layout=(2,2), ...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.