2

我的计划是有 4GridSpec(4,1)个网格规格来创建一个 4x4 的子图网格。我想在每行 4 个子图的 x 轴上添加一条水平线。我查看了 matplotlib.lines.Line2D 但无法真正弄清楚。有什么建议么?我试图在视觉上简化图片,使其看起来不像 16 个单独的图表。

在下图中,我只有前 2 个网格规范,但我希望它可以更好地了解我希望实现的目标。

谢谢!干杯

代码(图表部分):

#---the graph---
fig = plt.figure(facecolor='white')

gs1 = GridSpec(4,1)
gs1.update(left = 0.15, right = .3375 , wspace=0.02)

ax1 = plt.subplot(gs1[3,0])
ax2 = plt.subplot(gs1[2,0])
ax3 = plt.subplot(gs1[1,0])
ax4 = plt.subplot(gs1[0,0])



gs2 = GridSpec(4,1)
gs2.update(left = 0.3875, right = .575, wspace=.25)

ax1 = plt.subplot(gs2[3,0])
ax2 = plt.subplot(gs2[2,0])
ax3 = plt.subplot(gs2[1,0])
ax4 = plt.subplot(gs2[0,0])


show()

在此处输入图像描述

4

1 回答 1

2

基本上,这个想法是画一条线并允许该线延伸到当前的轴视图之外,在下面的示例中,我将该线绘制为红色以便更好地查看它。

此外,您的 8 个图可以绘制在一个嵌套循环中,这将更好地组织代码并使这个“跨子图的公共线”更容易实现:

X=[1,3,4,5]
Y=[3,4,1,3]
L=['A', 'B', 'C', 'D']
f=plt.figure(figsize=(10,16), dpi=100)
gs1 = gridspec.GridSpec(4,1)
gs1.update(left = 0.15, right = .3375 , wspace=0.02)
gs2 = gridspec.GridSpec(4,1)
gs2.update(left = 0.3875, right = .575, wspace=.25)
sp1 = [plt.subplot(gs1[i,0]) for i in range(4)]
sp2 = [plt.subplot(gs2[i,0]) for i in range(4)]
for sp in [sp1, sp2]:
    for ax in sp:
        ax.bar(range(len(L)), X, 0.35, color='r')
        ax.bar(np.arange(len(L))+0.35, Y, 0.35)
        ax.spines['right'].set_visible(False)
        ax.yaxis.set_ticks_position('left')
        ax.spines['top'].set_visible(False)
        ax.xaxis.set_ticks_position('bottom')
        if sp==sp1:
            ax.axis(list(ax.get_xlim())+list(ax.get_ylim())) #set the axis view limit
            ll=ax.plot((0,10), (0,0), '-r') #Let's plot it in red to show it better
            ll[0].set_clip_on(False) #Allow the line to extend beyond the axis view
plt.savefig('temp.png')            

在此处输入图像描述

于 2014-05-02T16:23:34.783 回答