【问题标题】:Can I tell python to put an existing figure in a new figure?我可以告诉 python 将现有图形放入新图形中吗?
【发布时间】:2016-05-21 04:57:34
【问题描述】:

创建一个特定的情节需要做很多工作,所以我想通过创建一个返回数字的函数f() 来自动完成。

我想调用这个函数,这样我就可以把结果放在一个子图中。无论如何我可以做到这一点吗?下面是一些解释我的意思的伪代码

figure_of_interest = f()

fig,ax = plt.subplots(nrows = 4,cols = 1)

ax[1].replace_with(figure_of_interest)

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    这是在herehere 之前提出的。

    简答:不可能。

    但您始终可以修改坐标区实例或使用函数来创建/修改当前坐标区:

    import matplotlib.pyplot as plt
    import numpy as np
    
    def test():
        x = np.linspace(0, 2, 100)
    
        # With subplots
        fig1, (ax1, ax2) = plt.subplots(2)
        plot(x, x, ax1)
        plot(x, x*x, ax2)
    
        # Another Figure without using axes
        fig2 = plt.figure()
        plot(x, np.exp(x))
    
        plt.show()
    
    def plot(x, y, ax=None):
        if ax is None:
            ax = plt.gca()
        line, = ax.plot(x, y)
        return line
    
    test()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-08
      • 2023-01-26
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 2011-12-05
      • 2019-08-20
      • 1970-01-01
      相关资源
      最近更新 更多