Python 尝试向现有Matplotlib地物添加其他轨迹

Python 尝试向现有Matplotlib地物添加其他轨迹,python,matplotlib,Python,Matplotlib,我使用的是一个函数,它可以输出一个验证数据的图形对象。我的脚本计算了一些模型参数,我想在现有地物对象上绘制这些参数。我该怎么做?每当我尝试绘制建模数据时,它都会在一个新窗口中进行绘制。我的代码是这样的: datafig = plotting_function(args) #Returning a figure object datafig.show() plt.plot([modeled_x],[modeled_y]) #Plotting in a new window 我尝试过使用plt

我使用的是一个函数,它可以输出一个验证数据的图形对象。我的脚本计算了一些模型参数,我想在现有地物对象上绘制这些参数。我该怎么做?每当我尝试绘制建模数据时,它都会在一个新窗口中进行绘制。我的代码是这样的:

datafig = plotting_function(args) #Returning a figure object

datafig.show()

plt.plot([modeled_x],[modeled_y]) #Plotting in a new window
我尝试过使用plt.hold()/plt.hold(True),但这没有任何作用。有什么想法吗

编辑:

MCVE:


MCVE的结果:

您可以在任何
plt.plt
(或任何其他
plt
绘图函数)调用之前,通过调用
plt.figure(my\u figure\u index)
指定要绘图到的图形

例如:

plt.figure(10) # creates new figure if doesn't exist yet
plt.plot(...) # plots in figure 10

plt.figure(2) # creates new figure if doesn't exist yet
plt.plot(...) # plots in this figure 2

plt.figure(10) # figure already exists, just makes it the active one
plt.plot(...) # plots in figure 10 (in addition to already existing stuff)

您需要指定要打印的轴<代码>plt.图(2)将生成一个数字为2的图,无论现有图是否具有该数字
axes_2.plot()
,但是将打印直接输入到
axes_2
上的任何数据以及已经存在的任何数据。如果未立即显示,则应在绘图函数后添加
plt.draw()


尽量不要混用plt、符号和ax符号,因为这将在以后造成混淆!如果你用的是无花果和斧头,那就坚持使用吧

谢谢你的评论。我已经给了你的建议一个镜头,Python只是用同样的#制作了另一个图形。没有错误或任何东西:作为参考,我使用Spyder。请注意,当我绘制“datafig”或已经存在的figure对象时,它默认为图2。不,只有一个!不明白为什么要使用
plt.close(“全部”)
。它使我的代码崩溃(
figure_2
None
)。如果没有它,效果会非常好……奇怪的是,也许这个问题是Spyder特有的。如果我上面的例子没有为你重现这个问题,那么听起来好像是在我这边。不管怎样,谢谢你的帮助!
plt.figure(10) # creates new figure if doesn't exist yet
plt.plot(...) # plots in figure 10

plt.figure(2) # creates new figure if doesn't exist yet
plt.plot(...) # plots in this figure 2

plt.figure(10) # figure already exists, just makes it the active one
plt.plot(...) # plots in figure 10 (in addition to already existing stuff)