Python plt.close() 和 plt.clf() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16661790/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:18:03  来源:igfitidea点击:

Difference between plt.close() and plt.clf()

pythonmatplotlib

提问by Joseph Clifford

In Python, what is the difference between plt.clf()and plt.close()?

在 Python 中,plt.clf()和之间有什么区别plt.close()

Will they function the same way?

他们会以同样的方式运作吗?

采纳答案by wim

plt.close()will close the figure window entirely, where plt.clf()will just clear the figure - you can still paint another plot onto it.

plt.close()将完全关闭图形窗口,在这里plt.clf()只会清除图形 - 您仍然可以在其上绘制另一个图。

It sounds like, for your needs, you should be preferring plt.clf(), or better yet keep a handle on the line objects themselves (they are returned in lists by plotcalls) and use .set_dataon those in subsequent iterations.

听起来,根据您的需要,您应该更喜欢plt.clf(),或者更好地保留行对象本身的句柄(它们通过plot调用在列表中返回)并.set_data在后续迭代中用于那些对象。

回答by Biplob45

plt.clf()clears the entire current figure with all its axes, but leaves the window opened, such that it may be reused for other plots.

plt.clf()清除整个当前图形及其所有轴,但保持窗口打开,以便它可以重新用于其他绘图。

plt.close()closes a window, which will be the current window, if not specified otherwise.

plt.close()关闭一个窗口,如果没有另外指定,它将是当前窗口。

回答by murnko

I think it is worth mentioning that plt.close()releases the memory, thus is preferred when generating and saving many figures in one run.

我认为值得一提的是plt.close()释放内存,因此在一次运行中生成和保存多个数字时首选。

Using plt.clf()in such case will produce a warning after 20 plots (even if they are not going to be shown by plt.show()):

plt.clf()在这种情况下使用将在 20 个图后产生警告(即使它们不会被 显示plt.show()):

More than 20 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory.

已经打开了20多个数字。通过 pyplot 接口 ( matplotlib.pyplot.figure)创建的图会保留直到明确关闭,并且可能会消耗太多内存。

回答by addy

There is a slight difference between the two functions.

这两个功能之间有细微的差别。

plt.close()- It altogether plots the graph in seperate windows,releasing memory,retaining each window for view.

plt.close()- 它在单独的窗口中一起绘制图形,释放内存,保留每个窗口以供查看。

plt.clf()- We can say,it displays the graph in the same window one after other

plt.clf()- 我们可以说,它在同一个窗口中一个接一个地显示图形

For illustration, I have plotted two graphs with paramters year and views on X axis and Y axis each. Initially I have used closed function.it displayed the graphs in two seperate windows…

为了说明,我绘制了两个图形,每个图形都带有参数 year 和 X 轴和 Y 轴上的视图。最初我使用的是封闭函数。它在两个单独的窗口中显示图形......

Afterwords, when I run the program with clf() it clears the graph and displays next one in same window i.e figure 1. Here is the code snippet -

后记,当我使用 clf() 运行程序时,它会清除图形并在同一窗口中显示下一个图形,即图 1。这是代码片段 -

    import matplotlib.pyplot as plt
    year = [2001,2002,2003,2004]
    Views= [12000,14000,16000,18000]
    Views2 = [15000,1800,24000,84000]
    plt.plot(year,Views)
    plt.show()
    plt.clf()
    plt.plot(year,Views2)
    plt.show()
    plt.clf()

image 1 with close function

图 1 具有关闭功能



image 2 with close function

图 2 具有关闭功能



image 1 with clf function

带有 clf 函数的图像 1



image 2 with clf function

带有 clf 函数的图像 2