是否可以在matplotlib中自动生成多个子图?

Dan*_*wer 2 matplotlib python-2.7 subplot

是否可以在matplotlib中自动生成多个子图?我想要自动化的过程的一个例子是:

import matplotlib.pyplot as plt
figure = plt.figure()
ax1 = figure.add_subplot(2, 3, 1)
ax2 = figure.add_subplot(2, 3, 2)
ax3 = figure.add_subplot(2, 3, 3)
ax4 = figure.add_subplot(2, 3, 4)
ax5 = figure.add_subplot(2, 3, 5)
ax6 = figure.add_subplot(2, 3, 6)
Run Code Online (Sandbox Code Playgroud)

子图需要唯一的名称,因为这将允许我做以下的事情:

for ax in [ax1, ax2, ax3, ax4, ax5, ax6]:
    ax.set_title("example")
Run Code Online (Sandbox Code Playgroud)

非常感谢.

另外:是否有任何功能可以自动生成多个子图?如果我需要重复上述过程100次怎么办?我是否必须输出每个ax1到ax100?

Rut*_*ies 6

您可以使用:

fig, axs = plt.subplots(2,3)
Run Code Online (Sandbox Code Playgroud)

axs将是一个包含子图的数组.

或立即解压缩阵列:

fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2,3)
Run Code Online (Sandbox Code Playgroud)

  • 如果第二个选项应该是图,([ax1,ax2,ax3],[ax4,ax5,ax6])= plt.subplots(2,3)或类似的东西,因为axs是2x3阵列? (2认同)