【问题标题】:How to put a bounding box around groups of subplots while using gridspec?使用gridspec时如何在子图组周围放置一个边界框?
【发布时间】:2017-09-28 15:07:24
【问题描述】:

我的目标是在每组 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')

【问题讨论】:

    标签: python-3.x matplotlib


    【解决方案1】:

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

    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,例如通过

    改变它的颜色
    outerax.set_facecolor(colors[i])
    outerax.patch.set_alpha(0.3)
    

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

    from matplotlib import pyplot as plt
    import matplotlib.gridspec as gridspec
    
    fig = plt.figure(figsize=(9,9))
    fig.suptitle(' title ', fontsize=12,
                 bbox={'facecolor':'none', 'alpha':0.5, 'pad':5})
    
    colors=["crimson", "indigo", "limegreen", "gold"]
    
    for i in range(4):
        #outer
        outergs = gridspec.GridSpec(1, 1)
        outergs.update(bottom=(i//2)*.47+0.01,left=(i%2)*.5+0.02, 
                       top=(1+i//2)*.47-0.01,  right=(1+i%2)*.5-0.02)
        outerax = fig.add_subplot(outergs[0])
        outerax.tick_params(axis='both',which='both',bottom=0,left=0,
                            labelbottom=0, labelleft=0)
        outerax.set_facecolor(colors[i])
        outerax.patch.set_alpha(0.3)
    
        #inner
        gs = gridspec.GridSpec(2, 2)
        gs.update(bottom=(i//2)*.47+0.05,left=(i%2)*.5+0.08, 
                       top=(1+i//2)*.47-0.05,  right=(1+i%2)*.5-0.05,
                       wspace=0.35, hspace=0.35)
        for k in range(4):
            ax = fig.add_subplot(gs[k])
            ax.set_title('Axes Title {}'.format(k+1), color=colors[i])
    
    plt.show()
    

    【讨论】:

    • 是的,成功了。使用 fig.add 是我所缺少的。谢谢!
    • 是否可以在边界框内但在内部绘图之外进行颜色填充?
    • @jchand20 不,我现在不在。
    猜你喜欢
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多