Python 如何在matplotlib中任意绘制多个子地块?

Python 如何在matplotlib中任意绘制多个子地块?,python,matplotlib,Python,Matplotlib,我通常在想要绘制两个图时执行此操作: f, (ax1, ax2) = plt.subplots(1, 2) 如果我有多个图像列表,并且希望对每个列表运行相同的函数,以便打印列表中的每个图像,该怎么办?每个列表都有不同数量的图像 那么: num_subplots = len(listoffigs) axs = [] for i in range(num_subplots): axs.append(plt.subplot(num_subplots, 1, i+1)) 甚至更短,如列表理解

我通常在想要绘制两个图时执行此操作:

f, (ax1, ax2) = plt.subplots(1, 2)
如果我有多个图像列表,并且希望对每个列表运行相同的函数,以便打印列表中的每个图像,该怎么办?每个列表都有不同数量的图像

那么:

num_subplots = len(listoffigs)
axs = []
for i in range(num_subplots):
    axs.append(plt.subplot(num_subplots, 1, i+1))
甚至更短,如列表理解:

num_subplots = len(listoffigs)
axs = [plt.subplot(num_subplots, 1, i+1) for i in range(num_subplots)]

*)编辑:添加列表理解解决方案

首先,多个列表或一个长列表之间没有区别,所以将所有列表合并为一个列表

biglist = list1 + list2 + list3
然后,您可以根据列表的长度确定所需的子批次数量。在最简单的情况下,制作一个正方形网格

n = int(np.ceil(np.sqrt(len(biglist))))
fig, axes = plt.subplots(n,n)
用你的图像填充网格

for i, image in enumerate(biglist):
    axes.flatten()[i].imshow(image)
对于其余轴,关闭脊椎

while i < n*n:
    axes.flatten()[i].axis("off")
    i += 1
而i
列表理解解决方案是我一直在寻找的解决方案,实际上比类似问题中的其他建议要快得多。但是如何将列表用作图形?您可以从列表索引中获得不同的子图。因此,
axs[0]
将引用第一个子批次。