我可以告诉 python 将现有图形放入新图形中吗? [英] Can I tell python to put an existing figure in a new figure?

查看:35
本文介绍了我可以告诉 python 将现有图形放入新图形中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个特定的图需要大量的工作,因此我想通过创建一个返回图形的函数 f()来自动执行此操作.

Creating a certain plot is a lot of work, so I would like to automate this by create a function f() that returns a figure.

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

I would like to call this function so that I can put the result in a subplot. Is there anyway I can do this? Below is some psuedo code explaining what I mean

figure_of_interest = f()

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

ax[1].replace_with(figure_of_interest)

推荐答案

这是在 此处此处

简短的回答:这不可能.

Short answer: It is not possible.

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

But you can always modify the axes instance or use a function to create/modify the current axes:

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()

这篇关于我可以告诉 python 将现有图形放入新图形中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆