将在函数中创建的图形添加到另一个图形的子图中

问题描述

我创建了两个函数来绘制两个特定的图并返回各自的数字:

import matplotlib.pyplot as plt

x = range(1,100)
y = range(1,100)

def my_plot_1(x,y):
    fig = plt.plot(x,y)
    return fig

def my_plot_2(x,y)
    return fig

现在,在我的函数之外,我想创建一个带有两个子图的图形并将我的函数图形添加到其中。像这样:

my_fig_1 = my_plot_1(x,y)
my_fig_2 = my_plot_2(x,y)

fig,fig_axes = plt.subplots(ncols=2,nrows=1)
fig_axes[0,0] = my_fig_1
fig_axes[0,1] = my_fig_2

但是,仅仅将创建的图形分配给这个新图形是行不通的。该函数调用图窗,但未在子图中分配。有没有办法将我的函数图放在另一个图的子图中?

解决方法

将您的函数传递给 struct S { S() {} S(int) {} }; template <typename T = S> void foo(T x) { std::cout << "S" << std::endl; } template <> void foo<S>(S x) { std::cout << "S spc" << std::endl; } int main() { foo({}); foo(0); } 更容易和更好:

Axes