Python matplotlib.pyplot 不会忘记以前的图 - 我如何刷新/刷新?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17106288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
matplotlib.pyplot will not forget previous plots - how can I flush/refresh?
提问by atomh33ls
How do you get matplotlib.pyplotto "forget" previous plots
你如何matplotlib.pyplot“忘记”以前的情节
I am trying to plot multiple time using matplotlib.pyplot
我正在尝试使用多次绘图 matplotlib.pyplot
The code looks like this:
代码如下所示:
def plottest():
import numpy as np
import matplotlib.pyplot as plt
a=np.random.rand(10,)
b=np.random.rand(10,)
c=np.random.rand(10,)
plt.plot(a,label='a')
plt.plot(b,label='b')
plt.plot(c,label='c')
plt.legend(loc='upper left')
plt.ylabel('mag')
plt.xlabel('element)')
plt.show()
e=np.random.rand(10,)
f=np.random.rand(10,)
g=np.random.rand(10,)
plt.plot(e,label='e')
plt.plot(f,label='f')
plt.plot(g,label='g')
plt.legend(loc='upper left')
plt.ylabel('mag')
plt.xlabel('element)')
plt.show()
Unfortunately I keep getting the same plot (actually from some other code which I ran and completed a while ago) no matter what I do.
不幸的是,无论我做什么,我都会得到相同的情节(实际上是从我不久前运行并完成的其他一些代码中获得的)。
Similar code has worked previously for me.
类似的代码以前对我有用。
I have looked at these questions:
我看过这些问题:
Matplotlib pyplot show() doesn't work once closed
Matplotlib pyplot show() 一旦关闭就不起作用
(python) matplotlib pyplot show() .. blocking or not?
(python) matplotlib pyplot show() .. 阻塞与否?
and tried using plt.show(), plt.clf()and plt.closeto no avail.
并尝试使用plt.show(),plt.clf()并plt.close无济于事。
Any ideas?
有任何想法吗?
采纳答案by Saullo G. P. Castro
I would rather use plt.clf()after every plt.show()to just clear the current figureinstead of closing and reopening it, keeping the window size and giving you a better performance and much better memory usage.
我宁愿使用plt.clf()after eachplt.show()来清除当前图形,而不是关闭并重新打开它,从而保持窗口大小并为您提供更好的性能和更好的内存使用率。
Similarly, you could do plt.cla()to just clear the current axes.
同样,您可以plt.cla()只清除当前轴。
To clear a specific axes, useful when you have multiple axes within one figure, you could do for example:
要清除特定的axes,当您在一个图形中有多个轴时很有用,您可以执行以下操作:
fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0, 1].clear()
回答by atomh33ls
I discovered that this behaviour only occurs after running a particular script, similar to the one in the question. I have no idea why it occurs.
我发现此行为仅在运行特定脚本后才会发生,类似于问题中的脚本。我不知道为什么会这样。
It works (refreshes the graphs) if I put
如果我把它工作(刷新图表)
plt.clf()
plt.cla()
plt.close()
after every plt.show()
每一次之后 plt.show()

