我可以告诉python将现有图形放在新图形中吗?
创建一个特定的绘图需要大量的工作,所以我想通过创建一个返回一个数字的函数我可以告诉python将现有图形放在新图形中吗?,python,matplotlib,Python,Matplotlib,创建一个特定的绘图需要大量的工作,所以我想通过创建一个返回一个数字的函数f()来自动完成这项工作 我想调用这个函数,这样我就可以把结果放在一个子图中。还有什么我可以做的吗?下面是一些psuedo代码,解释了我的意思 figure_of_interest = f() fig,ax = plt.subplots(nrows = 4,cols = 1) ax[1].replace_with(figure_of_interest) 这是之前和之后提出的问题 简短回答:这是不可能的 但您始终可以修改
f()
来自动完成这项工作
我想调用这个函数,这样我就可以把结果放在一个子图中。还有什么我可以做的吗?下面是一些psuedo代码,解释了我的意思
figure_of_interest = f()
fig,ax = plt.subplots(nrows = 4,cols = 1)
ax[1].replace_with(figure_of_interest)
这是之前和之后提出的问题
简短回答:这是不可能的
但您始终可以修改轴实例或使用函数创建/修改当前轴:
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()