Matplotlib - 将子图添加到子图中?

dan*_*n_g 23 python matplotlib figure subplot

我正在尝试创建一个由2x2网格组成的图形,其中每个象限中有2个垂直堆叠的子图(即2x1网格).但我似乎无法弄清楚如何实现这一目标.

我得到的最接近的是使用gridspec和一些丑陋的代码(见下文),但是因为gridspec.update(hspace=X)改变了所有子图的间距,我仍然不是我想要的.

理想情况下,我想要的是,使用下面的图片作为示例,减少每个象限内的子图之间的间距,同时增加顶部和底部象限之间的垂直间距(即1-3和2-4之间).

有没有办法做到这一点(使用或不使用gridspec)?我最初想象的是生成每个子子图网格(即每个2x1网格)并将它们插入更大的2x2子图网格中,但我还没想出如何将子图添加到子图中,如果有偶数的话一种方式.

在此输入图像描述

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
plt.figure(figsize=(10, 8))
gs = gridspec.GridSpec(4,2)
gs.update(hspace=0.4)
for i in range(2):
    for j in range(4):
        ax = plt.subplot(gs[j,i])
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        plt.tick_params(which='both', top='off', right='off')
        if j % 2 == 0:
            ax.set_title(str(i+j+1))
            ax.plot([1,2,3], [1,2,3])
            ax.spines['bottom'].set_visible(False)
            ax.get_xaxis().set_visible(False)   
        else:
            ax.plot([1,2,3], [3,2,1])
Run Code Online (Sandbox Code Playgroud)

Sue*_*ver 40

您可以使用SubplotSpec嵌套GridSpec.外部网格为2 x 2,内部网格为2 x 1.以下代码应为您提供基本概念.

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

fig = plt.figure(figsize=(10, 8))
outer = gridspec.GridSpec(2, 2, wspace=0.2, hspace=0.2)

for i in range(4):
    inner = gridspec.GridSpecFromSubplotSpec(2, 1,
                    subplot_spec=outer[i], wspace=0.1, hspace=0.1)

    for j in range(2):
        ax = plt.Subplot(fig, inner[j])
        t = ax.text(0.5,0.5, 'outer=%d, inner=%d' % (i,j))
        t.set_ha('center')
        ax.set_xticks([])
        ax.set_yticks([])
        fig.add_subplot(ax)

fig.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Jod*_*mak 25

您现在可以使用以下方式嵌套子图figure.subfigurehttps ://matplotlib.org/stable/gallery/subplots_axes_and_figures/subfigures.html

您可能还会发现subplot_mosaic有用,因为它支持嵌套: https ://matplotlib.org/stable/tutorials/provisional/mosaic.html

虽然嵌套总是有点痛苦,但可以说这两种方法都使该过程更容易、更灵活。

摘自上面的例子:

import matplotlib.pyplot as plt

fig = plt.figure(constrained_layout=True)

subfigs = fig.subfigures(2, 2)

for outerind, subfig in enumerate(subfigs.flat):
    subfig.suptitle(f'Subfig {outerind}')
    axs = subfig.subplots(2, 1)
    for innerind, ax in enumerate(axs.flat):
        ax.set_title(f'outer={outerind}, inner={innerind}', fontsize='small')
        ax.set_xticks([])
        ax.set_yticks([])
 
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 5

由于我不得不依赖 Suever 的答案,但必须根据我的需要修改它,我想我可能会做出贡献,以防有人发现它有帮助:

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

channelArrangement = [16, 17, 18 , 19 , 22, 25, 28 , 29 , 31]

fig = plt.figure(figsize=(10, 8))
outer = gridspec.GridSpec(1, 2, wspace=0.2, hspace=0.2)

for i in range(2):
    inner = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=outer[i],
                                             wspace=0.1, hspace=0.1)
    row     = 0
    col     = 0
    maxCol  = 3

    for chan in channelArrangement:
        ax = plt.Subplot(fig, inner[row,col])
        t= ax.text(0.5,0.5, 'outer=%d\nrow=%d\ncol=%d' % (i,row,col))
        ax.set_xticks([])
        ax.set_yticks([])
        t.set_ha('center')
        fig.add_subplot(ax)
        col += 1
        if col == maxCol:
            col = 0
            row += 1
plt.show()
Run Code Online (Sandbox Code Playgroud)

嵌套子图