创建某个图需要大量工作,因此我想通过创建一个返回图形的函数 f()
来自动执行此操作。
我想调用此函数,以便将结果放入子图中。无论如何我可以做到这一点吗?下面是一些解释我的意思的伪代码
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()
关于python - 我可以告诉 python 将现有图形放入新图形中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35322009/