Matplotlib offers three functions:cla() # Clear axis clf() # Clear figure close() # Close a figure windowThe documentation doesn‘t offer a lot of insight into what the difference between these functions is. When should I use each function and what exactly does it do?
They all do different things, since matplotlib uses a hierarchical order in which a figure window contains a figure which may consist of many axes. The three commands you mention therefore do the following:cla() clears an
axis, i.e. the currently active axis in the current figure. It leaves the other axes untouched.clf() clears the entire current figure with all its axes, but leaves the window opened, such that it may be reused for other plots.close() closes a window, which will be the current window, if not specified otherwise.Which functions suits you best depends thus on your use-case.The close() function furthermore allows one to specify which window should be closed. The argument can either be a number or name given to a window when it was created using figure(number_or_name) or it can be a figure instance fig obtained, i.e., usingfig = figure(). If no argument is given to close(), the currently active window will be closed. Furthermore, there is the syntax close(‘all‘), which closes all figures.