【问题标题】:Is it possible to automatically generate multiple subplots in matplotlib?是否可以在 matplotlib 中自动生成多个子图?
【发布时间】:2015-01-26 21:43:37
【问题描述】:

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 吗?

【问题讨论】:

  • 自动生成,什么意思?你能说得更具体点吗?
  • 要生成上述6个子图,我相信您只能通过为剩余的title,legend etc添加更多for循环来最小化代码。

标签: python-2.7 matplotlib subplot


【解决方案1】:

你可以使用:

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

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

或者立即解压数组:

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

【讨论】:

  • 第二个选项是否应该是 fig, ([ax1, ax2, ax3], [ax4, ax5, ax6]) = plt.subplots(2,3) 或类似的东西,因为 axs 是2x3 数组?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多