gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:15:46 25 4
gpt4 key购买 nike

是否可以在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?

最佳答案

您可以使用:

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

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

或者立即解压数组:

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

关于python-2.7 - 是否可以在matplotlib中自动生成多个子图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27185139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com