python - 我可以告诉 python 将现有图形放入新图形中吗?

标签 python matplotlib

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

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

figure_of_interest = f()

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

ax[1].replace_with(figure_of_interest)

最佳答案

这是在 here 之前被问到的和 here .

简答:不可能。

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

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 - 我可以告诉 python 将现有图形放入新图形中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35322009/

相关文章:

python - Django 最小化 JsonResponse 中的 json

python - 如何在 Python 中将 unicode 代码转换为字符串?

python - 如何调整透明背景图形的某些特征的颜色

python - 为具有代表 'group' 的多条线的图表创建图例

python - 理解列表中使用的 Pop() 未从初始列表中删除所有项目

python - 在 jitted 函数中两次反转 numpy 数组的 View 使函数运行得更快

python - 如何对OpenCV calcHist()直方图进行polyfit()?

python - 注释堆叠的条形图 matplotlib 和 pandas

python - Matplotlib set_data使我的程序卡住(请检查 'Updated issue')

python - 在 Mac OS X 10.8.5 (Mountain Lion) 上使用 Enthought Canopy python 编辑器运行 matplotlib.pyplot 时没有显示绘图窗口