34

In matplotlib.pyplot, what is the difference between plt.clf() and 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.

wim
  • 338,267
  • 99
  • 616
  • 750
Joseph Clifford
  • 365
  • 1
  • 3
  • 5
  • I'm confused when do I use `plt.clf()` vs `plt.cla()`? I have a general function that saves open figures (makes no assumptions, does not take the fig object) and I want inside of it to have a way to completely close everything after it saves so that when I start producing the next plot the next call to the save function doesn't save the same plot again by accident. Is it ok if I ALWAYS run `plt.clf()` vs `plt.cla()` in my save function no matter what? Does it always close everything? `plt.show()` did do that but in cluster that can lead to bugs. – Charlie Parker May 21 '22 at 14:44
  • related: https://stackoverflow.com/questions/741877/how-do-i-tell-matplotlib-that-i-am-done-with-a-plot – Charlie Parker May 21 '22 at 14:45
  • `plt.close()` worked for me. – Charlie Parker May 21 '22 at 14:51

4 Answers4

43

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

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.

wim
  • 338,267
  • 99
  • 616
  • 750
  • What needs do you mean? I'm curious because I'm trying to decide whether to paint on the same figure after clearing it or just have a new window. – jvriesem Sep 24 '21 at 23:21
  • @jvriesem There was a lot more context in the original question, but another user had edited it out. I've rolled back. It's usually easier to re-use the axes if you don't want to set up the ticks, labels, etc again every time. – wim Sep 25 '21 at 19:12
  • I'm confused when do I use `plt.clf()` vs `plt.cla()`? I have a general function that saves open figures (makes no assumptions, does not take the fig object) and I want inside of it to have a way to completely close everything after it saves so that when I start producing the next plot the next call to the save function doesn't save the same plot again by accident. Is it ok if I ALWAYS run `plt.clf()` vs `plt.cla()` in my save function no matter what? Does it always close everything? `plt.show()` did do that but in cluster that can lead to bugs. – Charlie Parker May 21 '22 at 14:44
10

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

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()):

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.

murnko
  • 148
  • 1
  • 10
  • I'm confused when do I use `plt.clf()` vs `plt.cla()`? I have a general function that saves open figures (makes no assumptions, does not take the fig object) and I want inside of it to have a way to completely close everything after it saves so that when I start producing the next plot the next call to the save function doesn't save the same plot again by accident. Is it ok if I ALWAYS run `plt.clf()` vs `plt.cla()` in my save function no matter what? Does it always close everything? `plt.show()` did do that but in cluster that can lead to bugs. – Charlie Parker May 21 '22 at 14:45
2

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.close() closes a window, which will be the current window, if not specified otherwise.

kvorobiev
  • 5,012
  • 4
  • 29
  • 35
Biplob45
  • 31
  • 4
  • I'm confused when do I use `plt.clf()` vs `plt.cla()`? I have a general function that saves open figures (makes no assumptions, does not take the fig object) and I want inside of it to have a way to completely close everything after it saves so that when I start producing the next plot the next call to the save function doesn't save the same plot again by accident. Is it ok if I ALWAYS run `plt.clf()` vs `plt.cla()` in my save function no matter what? Does it always close everything? `plt.show()` did do that but in cluster that can lead to bugs. – Charlie Parker May 21 '22 at 14:46
2

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.clf() - We can say,it displays the graph in the same window one after other

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…

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 -

    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


image 2 with close function


image 1 with clf function


image 2 with clf function


Aditi
  • 53
  • 8
  • I'm confused when do I use `plt.clf()` vs `plt.cla()`? I have a general function that saves open figures (makes no assumptions, does not take the fig object) and I want inside of it to have a way to completely close everything after it saves so that when I start producing the next plot the next call to the save function doesn't save the same plot again by accident. Is it ok if I ALWAYS run `plt.clf()` vs `plt.cla()` in my save function no matter what? Does it always close everything? `plt.show()` did do that but in cluster that can lead to bugs. – Charlie Parker May 21 '22 at 14:45