首页 > 解决方案 > 如何创建一个 3X5 子图网格,其中第一列和第一行的前 2 个单元格合并为一个大子图?

问题描述

如何创建一个最初有 15 个子图的子图网格,但其中 4 个子图合并到一个更大的子图中,这样实际上只有 12 个子图:11 个较小的子图和一个较大的子图。我在这里附上了一张我用 Photoshop 创建的图像: 3x5 子图网格,左上角有一个更大的子图

标签: pythonmatplotlibgridsubplot

解决方案


改编自使用 GridSpec 和其他函数自定义图形布局

import matplotlib.pyplot as plt 
import numpy as np                                                                
from itertools import product 

# create a figure, use the best avalable layout and add a gridspec
fig = plt.figure(constrained_layout=True) 
gs = fig.add_gridspec(3, 5) 

# three actions

# create a larger subplot that spans rows 0 to 2 (that is, 0 and 1)
# and columns 0 to 2 (again that is 0 and 1)
ax_left_top = fig.add_subplot(gs[0:2,0:2]) 

# now the axes on the right, we put them in a list,
# the rows are 0,1, the columns 2,3,4 
axes_right = [fig.add_subplot(gs[r,c]) for r,c in product((0,1),(2,3,4))]

# finally a list containing the axes on the bottom, the row #2
# and all the columns 
axes_bottom = [fig.add_subplot(gs[2,c]) for c in (0, 1, 2, 3, 4)]                 

# label the axes for your reference
ax_left_top.annotate('ax_left_top', (.1,.5))
for n, ax in enumerate(axes_right):
    ax.annotate('axes_right[%d]'%n, (0.3,0.5))
for n, ax in enumerate(axes_bottom):
    ax.annotate('axes_bottom[%d]'%n, (0.2,0.5))

在此处输入图像描述


推荐阅读