简体   繁体   English

如何管理熊猫的子图?

[英]How to manage subplots in Pandas?

My DataFrame is: 我的DataFrame是:

df = pd.DataFrame({'A': range(0,-10,-1), 'B': range(10,20), 'C': range(10,30,2)})

and plot: 并绘制:

df[['A','B','C']].plot(subplots=True, sharex=True)

I get one column with 3 subplots, each even height. 我得到一列包含3个子图,每个子图的高度均等。

How to plot it this way that I have only two subplots where 'A' is in upper one and 'B' and 'C' are in lower and lower graph's height is different than height of graph 'A' (x axis is shared)? 如何以这种方式绘制图,我只有两个子图,其中“ A”在上方,而“ B”和“ C”在下方, 并且下图的高度与图“ A”的高度不同(x轴是共享的) ?

Use subplots with gridspec_kw parmater to setup your grid then use the ax paramter in pandas plot to use those axes defined in your subplots statement: subplotsgridspec_kw参数结合使用以设置网格,然后在pandas图中使用ax参数来使用子图语句中定义的轴:

f, ax = plt.subplots(2,2, gridspec_kw={'height_ratios':[1,2]})
df[['A','B','C']].plot(subplots=True, sharex=True, ax=[ax[0,0],ax[0,1],ax[1,0]])
ax[1,1].set_visible(False)

Output: 输出:

在此处输入图片说明

For clarity I post my modified code here: 为了清楚起见,我在这里发布了修改后的代码:

f, ax = plt.subplots(2,1, sharex=True, gridspec_kw={'height_ratios':[1,3]})
f.subplots_adjust(hspace=0)
df[['A','B','C']].plot(subplots=True, ax=[ax[0],ax[1],ax[1]])

That will do it. 这样就可以了。 Thanks. 谢谢。

在此处输入图片说明

I was able to do it with .subplot2grid() . 我能够使用.subplot2grid()做到这一点 Which only creates 3 plots as needed. 仅根据需要创建3个地块。

ax1 = plt.subplot2grid((3, 2), (0, 0), colspan=1)
ax2 = plt.subplot2grid((3, 2), (0, 1), colspan=1)
ax3 = plt.subplot2grid((3, 2), (1, 0), rowspan=2, sharex=ax1)
plt.setp(ax1.get_xticklabels(), visible=False)

ax1.plot(df['A'])
ax2.plot(df['B'], color='darkorange')
ax3.plot(df['C'], color='green')

Output: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM