简体   繁体   English

plt.close() 和 plt.clf() 之间的区别

[英]Difference between plt.close() and plt.clf()

In matplotlib.pyplot , what is the difference between plt.clf() and plt.close() ?matplotlib.pyplot中, plt.clf()plt.close()有什么区别? Will they function the same way?它们会以同样的方式运作吗?

I am running a loop where at the end of each iteration I am producing a figure and saving the plot.我正在运行一个循环,在每次迭代结束时我都会生成一个图形并保存该图。 On first couple tries the plot was retaining the old figures in every subsequent plot.在第一次尝试中,情节在随后的每个情节中都保留了旧数字。 I'm looking for, individual plots for each iteration without the old figures, does it matter which one I use?我正在寻找没有旧数字的每次迭代的单独图,我使用哪一个重要吗? The calculation I'm running takes a very long time and it would be very time consuming to test it out.我正在运行的计算需要很长时间,并且测试它会非常耗时。

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 plot calls) and use .set_data on those in subsequent iterations.听起来,根据您的需要,您应该更喜欢plt.clf() ,或者更好地保留线对象本身的句柄(它们通过plot调用在列表中返回)并在后续迭代中使用.set_data

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.已经打开了20多个数字。 Figures created through the pyplot interface ( matplotlib.pyplot.figure ) are retained until explicitly closed and may consume too much memory.通过 pyplot 接口 ( matplotlib.pyplot.figure ) 创建的图形会一直保留到显式关闭,并且可能会消耗太多内存。

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()关闭一个窗口,如果没有另外指定,它将是当前窗口。

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.为了说明,我绘制了两个带有参数 year 的图表,每个图表分别在 X 轴和 Y 轴上。 Initially I have used closed function.it displayed the graphs in two seperate windows…最初我使用了封闭函数。它在两个单独的窗口中显示图形……

Afterwords, when I run the program with clf() it clears the graph and displays next one in same window ie 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()

图 1 具有关闭功能


图 2 具有关闭功能


图 1 具有 clf 功能


图 2 带有 clf 函数


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM