Python 如何在一个图形中生成超过10个子图?

Python 如何在一个图形中生成超过10个子图?,python,matplotlib,figure,subplot,Python,Matplotlib,Figure,Subplot,我正在尝试创建一个5x4的子地块网格,通过查看示例,我认为最好的方法是: import matplotlib.pyplot as plt plt.figure() plt.subplot(221) 其中,子批次(22)中的前两个数字表示它是2x2网格,第三个数字表示您正在制作4个网格中的哪一个。然而,当我尝试这一点时,我不得不走到: plt.subplot(5420) 我得到了一个错误: ValueError: Integer subplot specification must be a

我正在尝试创建一个5x4的子地块网格,通过查看示例,我认为最好的方法是:

import matplotlib.pyplot as plt
plt.figure()
plt.subplot(221)
其中,子批次(22)中的前两个数字表示它是2x2网格,第三个数字表示您正在制作4个网格中的哪一个。然而,当我尝试这一点时,我不得不走到:

plt.subplot(5420)
我得到了一个错误:

ValueError: Integer subplot specification must be a three digit number.  Not 4
那么这是否意味着你不能制作超过10个子图,或者有办法解决这个问题,或者我误解了它的工作原理


提前谢谢。

您可能正在寻找。可以说明栅格的大小(5,4)和每个绘图的位置(行=0,列=2,即-0,2)。检查以下示例:

import matplotlib.pyplot as plt

plt.figure(0)
ax1 = plt.subplot2grid((5,4), (0,0))
ax2 = plt.subplot2grid((5,4), (1,1))
ax3 = plt.subplot2grid((5,4), (2, 2))
ax4 = plt.subplot2grid((5,4), (3, 3))
ax5 = plt.subplot2grid((5,4), (4, 0))
plt.show()
,这导致:

应构建嵌套循环以形成完整网格:

import matplotlib.pyplot as plt

plt.figure(0)
for i in range(5):
    for j in range(4):
        plt.subplot2grid((5,4), (i,j))
plt.show()
,您将获得以下信息:

绘图的工作原理与任何子绘图中的工作原理相同(直接从已创建的轴调用它):

,导致:

请注意,可以为绘图提供不同的大小(说明每个绘图的列数和行数):

因此:


在我一开始给出的链接中,您还可以找到删除标签的示例。

使用逗号:
plt.subplot(5,4,20)
。您可以在中找到此行为。也与此相关(尽管是低质量问题):为什么包含
plot=[]
?在这种情况下有什么用处?
import matplotlib.pyplot as plt
import numpy as np

plt.figure(0)
plots = []
for i in range(5):
    for j in range(4):
        ax = plt.subplot2grid((5,4), (i,j))
        ax.scatter(range(20),range(20)+np.random.randint(-5,5,20))
plt.show()
import matplotlib.pyplot as plt

plt.figure(0)
ax1 = plt.subplot2grid((3,3), (0,0), colspan=3)
ax2 = plt.subplot2grid((3,3), (1,0), colspan=2)
ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3,3), (2, 0))
ax5 = plt.subplot2grid((3,3), (2, 1))
plt.show()