Python绘制两个子图

2024-09-30 14:21:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组12个地块,我想保存为一个。第一组是由9个片段组成的3x3子片段,第二部分是4个片段的2x2子片段。 我试着在这两幅图之间添加一个子图(121),并尝试使用图(1)和图(2),但两幅图都不符合我的要求,将这两幅图像保存为一幅大图像。有什么简单的方法吗?在

plt.subplot(331)
plt.imshow(getpoly(seg1),origin="lower")
plt.subplot(332)
plt.imshow(getpoly(seg2),origin="lower")
plt.subplot(333)
plt.imshow(getpoly(seg3),origin="lower")
plt.subplot(334)
plt.imshow(getpoly(seg4),origin="lower")
plt.subplot(335)
plt.imshow(getpoly(seg5),origin="lower")
plt.subplot(336)
plt.imshow(getpoly(seg6),origin="lower")
plt.subplot(337)
plt.imshow(getpoly(seg7),origin="lower")
plt.subplot(338)
plt.imshow(getpoly(seg8),origin="lower")
plt.subplot(339)
plt.imshow(getpoly(seg9),origin="lower")


plt.subplot(221)
plt.imshow(h1,origin="lower")
plt.colorbar()
plt.subplot(222)
plt.imshow(h2,origin="lower")
plt.colorbar()
plt.subplot(223)
plt.imshow(getpoly(h2),origin="lower")
plt.colorbar()  
plt.subplot(224)
plt.imshow(h1-getpoly(h2),origin="lower")
plt.colorbar()

Tags: 方法图像plth2originh1lowerimshow
1条回答
网友
1楼 · 发布于 2024-09-30 14:21:53

您可能希望将gridspecGridSpecFromSubplotSpec一起使用,如here所示。在

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

gs = gridspec.GridSpec(1, 2)
gs0 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs[0])
gs1 = gridspec.GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1])

fig = plt.figure()

for i in range(9):
    ax = fig.add_subplot(gs0[i//3, i%3])
    ax.imshow(np.random.rand(4,4))
    ax.set_xticks([]); ax.set_yticks([])

for i in range(4):
    ax = fig.add_subplot(gs1[i//2, i%2])
    ax.imshow(np.random.rand(4,4))
    ax.set_xticks([]); ax.set_yticks([])

plt.show()

enter image description here

相关问题 更多 >