2013-11-20 22 views
2

我想把子图添加到子图的底部。我得到的问题是,所有第一组子图都想分享他们的X轴,但不是最底层的。当别人做的时候添加一个不共享x轴的子图

channels是想要共享x轴的子图。

那么如何添加一个不共享x轴的子图呢? 这是我的代码:

def plot(reader, tdata): 
    '''function to plot the channels''' 

channels=[] 
     for i in reader: 
     channels.append(i) 

    fig, ax = plt.subplots(len(channels)+1, sharex=False, figsize=(30,16), squeeze=False) 
    plot=0 
    #where j is the channel name 
    for i, j in enumerate(reader): 

     y=reader["%s" % j] 
     ylim=np.ceil(np.nanmax(y)) 
     x=range(len((reader["%s" % j]))) 
     ax[plot,0].plot(y, lw=1, color='b') 
     ax[plot,0].set_title("%s" % j) 
     ax[plot,0].set_xlabel('Time/s') 
     ax[plot,0].set_ylabel('%s' % units[i]) 
     ax[plot,0].set_ylim([np.nanmin(y), ylim+(ylim/100)*10]) 
     plot=plot+1 

    ###here is the new subplot that doesn't want to share the x axis### 
    ax[plot, 0].plot() 
    plt.tight_layout() 
    plt.show() 

此代码工作,因为它们是共享型的最后一个次要情节的x轴。通道长度的变化取决于我在代码中前面指定的内容。

以某种方式使用add_subplot这是一个有效的选项,尽管我没有固定的频道数量?

非常感谢任何帮助

编辑 图片乔:

enter image description here

回答

3

这是最简单,直接在这种情况下使用fig.add_subplot

作为一个简单的例子:

import matplotlib.pyplot as plt 

fig = plt.figure(figsize=(6, 8)) 

# Axes that share the x-axis 
ax = fig.add_subplot(4, 1, 1) 
axes = [ax] + [fig.add_subplot(4, 1, i, sharex=ax) for i in range(2, 4)] 

# The bottom independent axes 
axes.append(fig.add_subplot(4, 1, 4)) 

# Let's hide the tick labels for all but the last shared-x axes 
for ax in axes[:2]: 
    plt.setp(ax.get_xticklabels(), visible=False) 

# And plot on the first subplot just to demonstrate that the axes are shared 
axes[0].plot(range(21), color='lightblue', lw=3) 

plt.show() 

enter image description here

+0

当我运行这段代码,规模不会改变绘制的范围内,我的共享轴的比例保持在0.06。我可以给你看一张图片,但不知道在哪里发布。 –

+0

@AshleighClayton - 你可以将它添加到你的问题中,或者只是发布在imgur(或类似的图像共享网站)上,并添加一个链接作为评论。我可能会误解你想要做的事情。 –

+0

我完全抄袭你的代码,只是为了测试它。我会把这个图像放在我的问题中。感谢您的帮助 –

相关问题