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

时间:2014-11-28 09:06:14

标签: python-2.7 matplotlib 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)

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

for ax in [ax1, ax2, ax3, ax4, ax5, ax6]:
    ax.set_title("example")

非常感谢。

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

1 个答案:

答案 0 :(得分:5)

您可以使用:

fig, axs = plt.subplots(2,3)

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

或立即解压缩阵列:

fig, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2,3)
相关问题