您可能正在寻找GridSpec http://matplotlib.org/users/gridspec.html。您可以指定网格的大小 (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()
,你会得到这个:
这些图的工作方式与任何子图相同(直接从您创建的轴调用它):
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()
, 所以:
在我一开始给出的链接中,您还会找到删除标签等内容的示例。