问题描述
在pyplot中,当我运行plt.plot(x)
时,它似乎在内部运行以下逻辑:
In pyplot, when I run plt.plot(x)
, it seems to internally run the following logic:
1)如果一个图形已经打开,请使用该图形,否则请创建一个新图形.
2)如果该图中的轴已经打开,请使用该轴,否则请创建一个新轴.
3) 在该轴上绘图.
1) If a figure is already open, use that figure, otherwise create a new one.
2) If an axes in that figure is already open, use that axes, otherwise create a new one.
3) Plot on that axis.
我想知道如何在下次调用某些绘图命令时强制步骤 (1) 打开一个新图形.类似的东西
I'm wondering how I can force step (1) to open up a new figure the next time some plotting command is called. Something like
plt.plot(x1)
with new_figure_environment(): # Ensure that plot commands within this go to new figures
some_function()
plt.show()
如果 some_function
包含:
def some_function():
plt.plot(x1)
然后我想打开一个新图形,但如果它不包含绘图功能,那么我不.
Then I want to open up a new figure, but if it does not contain a plot function then I do not.
推荐答案
如果您想使用 fig1 = plt.figure(1)
, fig2 以特定图形编号绘制图形= plt.figure(2)
等.要在特定图形中绘制图形,请定义轴 ax1 = fig1.gca()
gca =获取当前轴,而不是使用 plt.plot()
,请使用 ax1.plot()
绘制在图 1
If you want to plot in a specific figure number your figure using fig1 = plt.figure(1)
, fig2 = plt.figure(2)
etc.To plot a graph in a specific figure define axes ax1 = fig1.gca()
gca = get current axis and instead of using plt.plot()
use ax1.plot()
to plot in the figure 1
import matplotlib.pyplot as plt
x1 = [0,1]
x2 = [0,2]
y1 = [0,1]
y2 = [0,-1]
fig1 = plt.figure(1)
ax1 = fig1.gca()
fig2 = plt.figure(2)
ax2 = fig2.gca()
ax1.plot(x1,y1,'b')
ax2.plot(x2,y2,'r')
plt.show()
如果要创建5个数字,请使用列表:
If you want to create 5 figures use lists :
fig = []
ax = []
for i in range(5) :
fig.append(plt.figure(i))
ax.append(fig[i].gca())
如果图1已经打开,并且您想绘制其他曲线,则只需键入以下线条:
if the figure 1 is already opened and you want to plot an additional curve you just have to type these lines :
fig3 = plt.figure(1)
ax3 = fig1.gca()
ax3.plot(x1,y2,'g')
fig3.canvas.draw()
这篇关于Matplotlib.pyplot:打开新图形环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!