【问题标题】:Creating subplots from list of pyplot axes从 pyplot 轴列表创建子图
【发布时间】:2020-05-29 09:04:13
【问题描述】:

我想从绘图函数获得的轴列表中创建一个绘图:

import matplotlib.pyplot as plt
def func():
    x = range(10)
    y = range(10)
    plt.plot(x,y)
    return plt.gca()

fig, axs = plt.subplots(nrows=1, ncols=4)
for ax in axs:
    ax = func()
plt.show()

但是,我得到的图如下,并且在最后一个子图中包含所有图表:

如果可能的话,我想按原样使用绘图功能,而不是更改它。那可能吗?非常感谢!

【问题讨论】:

  • ax 传递给您的函数并直接对其进行操作。
  • 谢谢!我知道这个解决方案,但想在 for 循环中设置轴,而不是通过将轴对象传递给函数来对其进行操作。这有可能吗?
  • 检查plt.sca()
  • plt.sca() 需要一个轴作为参数。在做plt.sca(plt.gca()) 时,我得到与上述相同的结果(自然)
  • 是的plt.sca(ax)

标签: python matplotlib plot scipy


【解决方案1】:

感谢ImportanceOfBeingErnest 可以找到解决方案:

import matplotlib.pyplot as plt
def func():
    x = range(10)
    y = range(10)
    plt.plot(x,y)
    return plt.gca()

fig, axs = plt.subplots(nrows=1, ncols=4, figsize=(5,3))
for ax in axs:
    plt.sca(ax)
    ax = func()
plt.tight_layout()
plt.show()

现在,我明白了:

【讨论】:

    猜你喜欢
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 2017-05-27
    相关资源
    最近更新 更多