在子图网格中重新定位子图

2023-11-25

I am trying to make a plot with 7 subplots. At the moment I am plotting two columns, one with four plots and the other with three, i.e. like this:enter image description here

我按以下方式构建该图:

    #! /usr/bin/env python
    import numpy as plotting
    import matplotlib
    from pylab import *
    x = np.random.rand(20)
    y = np.random.rand(20)
    fig = figure(figsize=(6.5,12))
    subplots_adjust(wspace=0.2,hspace=0.2)
    iplot = 420
    for i in range(7):
       iplot += 1
       ax = fig.add_subplot(iplot)
       ax.plot(x,y,'ko')
       ax.set_xlabel("x")
       ax.set_ylabel("y")
    savefig("subplots_example.png",bbox_inches='tight')

然而,对于出版来说,我认为这看起来有点难看——我想做的是将最后一个子图移到两列之间的中心。那么,调整最后一个子图的位置以使其居中的最佳方法是什么? IE。将前 6 个子图放在 3X2 网格中,最后一个子图位于两列之间的中心。如果可以的话,我希望能够保留for循环以便我可以简单地使用:

    if i == 6:
       # do something to reposition/centre this plot     

使用网格规格(doc)使用 4x4 网格,并且每个图跨 2 列,如下所示:

import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(4, 4)
ax1 = plt.subplot(gs[0, 0:2])
ax2 = plt.subplot(gs[0,2:])
ax3 = plt.subplot(gs[1,0:2])
ax4 = plt.subplot(gs[1,2:])
ax5 = plt.subplot(gs[2,0:2])
ax6 = plt.subplot(gs[2,2:])
ax7 = plt.subplot(gs[3,1:3])
fig = gcf()
gs.tight_layout(fig)
ax_lst = [ax1,ax2,ax3,ax4,ax5,ax6,ax7]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在子图网格中重新定位子图 的相关文章

随机推荐