首页 > 解决方案 > 创建 3x3 matplotlib 子图,但在某些位置缺少图

问题描述

假设我有 6 个图,我想以通常的方式(plt.subplots 等)安排到一个子图设置中。

但是出于可视化原因,我需要按以下方式排列它们:

|1 0 0 |

|1 1 0 |

|1 1 1 | 

其中 1 表示我想要一个情节,0 表示我不想要。我不完全确定如何使用 matplotlib 中的子图做到这一点。任何建议都会很棒-谢谢!

标签: pythonmatplotlib

解决方案


使用Figure.add_gridspecFigure.add_subplot

>>> import matplotlib.pyplot as plt
>>> fig = plt.figure(constrained_layout=True)
>>> gs = fig.add_gridspec(3, 3)
>>> axes = [fig.add_subplot(gs[x,y])
                for x in range(3) for y in range(3) if x >= y]
>>> plt.show()

在此处输入图像描述


推荐阅读