假设我有6个图,我想用通常的方式(plt.subplots等)排列成一个子图。
然而,出于可视化的原因,我需要以以下方式安排它们:
|1 0 0 |
|1 1 0 |
|1 1 1 | 其中1表示我想要在那里绘制图,0表示我不想要。我不完全确定如何在matplotlib中使用子图。任何建议都会很棒--谢谢!
发布于 2021-06-15 20:38:16
使用Figure.add_gridspec和Figure.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()

发布于 2021-06-15 20:40:06
您可以在想要留空的点上填充空白子图,如下所示:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
fig, ((ax1, ax2, ax3), (ax4, ax5, ax6), (ax7, ax8, ax9)) = plt.subplots(3, 3)
fig.suptitle('Sharing x per column, y per row')
ax1.plot(x, y)
ax2.axis("off")
ax3.axis("off")
ax4.plot(x, -y, 'tab:green')
ax5.plot(x, -y**2, 'tab:red')
ax6.axis("off")
ax7.plot(x, -y, 'tab:green')
ax8.plot(x, -y**2, 'tab:red')
ax9.plot(x, -y**2, 'tab:red')发布于 2021-06-15 20:41:52
您可以创建3x3的子图,并使某些子图的轴不可见:
fig, axs = plt.subplots(3,3)
fig.tight_layout(pad=2.0)
axs[0,1].axis('off')
axs[0,2].axis('off')
axs[1,2].axis('off')输出:

https://stackoverflow.com/questions/67986228
复制相似问题