如何在使用gridspec时在一组子图周围放置一个边界框?

时间:2017-09-28 15:07:25

标签: python-3.x matplotlib

我的目标是在每组4个子图周围放置一个边界框。我似乎无法弄清楚如何使用我的gridspec子图。我遇到的例子涉及单个子图,但不是总格栅。

(我选择使用gridspec制作这样的图,这样我就能控制子图组之间的间距)

from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec



fig = plt.figure(figsize=(16,16))

fig.suptitle(' title ', fontsize=12,
             bbox={'facecolor':'none', 'alpha':0.5, 'pad':5})

gs = gridspec.GridSpec(2, 2)


gs.update(top=.48,left=0.1, right=0.48, wspace=0.15,hspace=0.2)
ax1 = plt.subplot(gs[0])
ax1.set_title('axes title')

ax2 = plt.subplot(gs[1])
ax2.set_title('axes title')

ax3 = plt.subplot(gs[2])
ax3.set_title('axes title')

ax4 = plt.subplot(gs[3])
ax4.set_title('axes title')

#New Gridspec
gs1 = gridspec.GridSpec(2, 2)
gs1.update(bottom=.53,left=0.55, right=0.9, hspace=0.2, wspace=.15)

ax5 = plt.subplot(gs1[0])
ax5.set_title('axes title')

ax6 = plt.subplot(gs1[1])
ax6.set_title('axes title')

ax7 = plt.subplot(gs1[2])
ax7.set_title('axes title')

ax8 = plt.subplot(gs1[3])
ax8.set_title('axes title')


#New Gridspec
gs2 = gridspec.GridSpec(2, 2)
gs2.update(top=.48,left=0.55, right=0.9, hspace=0.2, wspace=.15)

ax9 = plt.subplot(gs2[0])
ax9.set_title('axes title')

ax10 = plt.subplot(gs2[1])
ax8.set_title('axes title')

ax11 = plt.subplot(gs2[2])
ax11.set_title('axes title')

ax12 = plt.subplot(gs2[3])
ax12.set_title('axes title')

#New Gridspec

gs3 = gridspec.GridSpec(2, 2)
gs3.update(bottom=.53,left=0.1, right=0.48, wspace=0.15,hspace=0.2)

ax13 = plt.subplot(gs3[0])
ax13.set_title('axes title')

ax14 = plt.subplot(gs3[1])
ax14.set_title('axes title')

ax15 = plt.subplot(gs3[2])
ax15.set_title('axes title')

ax16 = plt.subplot(gs3[3])
ax16.set_title('axes title')



plt.savefig('test.png',bbox_inches='tight')

1 个答案:

答案 0 :(得分:4)

围绕多个子图创建框架或边框的选项可能是向图中添加另一个轴,该轴比相应的gridspec区域的范围大一些。

401 Unauthorized

为了使其工作,必须通过
创建所有其他轴 outergs = gridspec.GridSpec(1, 1) outergs.update(bottom=.50,left=0.07, right=0.50,top=0.93) outerax = fig.add_subplot(outergs[0]) outerax.tick_params(axis='both',which='both',bottom=0,left=0, labelbottom=0, labelleft=0) 代替ax_i = fig.add_subplot(gs...)

您当然可以根据自己的喜好修改ax_i = plt.subplot(gs...),例如通过

改变颜色
outerax

enter image description here

重现以上内容的完整代码:

outerax.set_facecolor(colors[i])
outerax.patch.set_alpha(0.3)