Asked  7 Months ago    Answers:  5   Viewed   71 times

Matplotlib offers these functions:

cla()   # Clear axis
clf()   # Clear figure
close() # Close a figure window

The 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. Additionally, there are functions from the pyplot interface and there are methods on the Figure class. I will discuss both cases below.

pyplot interface

pyplot is a module that collects a couple of functions that allow matplotlib to be used in a functional manner. I here assume that pyplot has been imported as import matplotlib.pyplot as plt. In this case, there are three different commands that remove stuff:

plt.cla() clears an axes, i.e. the currently active axes in the current figure. It leaves the other axes untouched.

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.

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.

methods of the Figure class

Additionally, the Figure class provides methods for clearing figures. I'll assume in the following that fig is an instance of a Figure:

fig.clf() clears the entire figure. This call is equivalent to plt.clf() only if fig is the current figure.

fig.clear() is a synonym for fig.clf()

Note that even del fig will not close the associated figure window. As far as I know the only way to close a figure window is using plt.close(fig) as described above.

Tuesday, June 1, 2021
answered 7 Months ago

There is no performance benefit offered by forEach. In fact, if you look at the source code, the forEach function actually simply performing for-in. For release builds, the performance overhead of this function over simply using for-in yourself is immaterial, though for debug builds, it results in an observable performance impact.

The main advantage of forEach is realized when you are doing functional programming, you can add it to a chain of functional calls, without having to save the prior result into a separate variable that you'd need if you used for-in syntax. So, instead of:

let objects = { ... }
    .filter { ... }

for object in objects {

You can instead stay within functional programming patterns: { ... }
    .filter { ... }
    .forEach { ... }

The result is functional code that is more concise with less syntactic noise.

FWIW, the documentation for Array, Dictionary, and Sequence all remind us of the limitations introduced by forEach, namely:

  1. You cannot use a break or continue statement to exit the current call of the body closure or skip subsequent calls.

  2. Using the return statement in the body closure will exit only from the current call to body, not from any outer scope, and won't skip subsequent calls.

Wednesday, July 28, 2021
answered 5 Months ago

There are many options for line styles and marker in MPL. Have a look here, here and here.

For your specific example (I quickly made up some functions and roughly plotted the first few examples):

import matplotlib.pyplot as plt
import numpy as np





This should give you something like this.

enter image description here

Friday, July 30, 2021
answered 4 Months ago
  • ax.clear() clears the axes. That is, it removes all settings and data from the axes such that you are left with an axes, just as it had been just created.

  • ax.axis("off") turns the axes off, such that all axes spines and ticklabels are hidden.

  • ax.set_visible(False) turns the complete axes invisible, including the data that is in it.

  • ax.remove() removes the axes from the figure.

Complete example:

import matplotlib.pyplot as plt

fig,axes = plt.subplots(2,3)
for ax in axes.flat:


enter image description here

Tuesday, September 7, 2021
answered 3 Months ago

The point you are looking for is there, but hidden "inside" the surface. This is a common problem in matplotlib.

I see two options here:

  1. Make the surface plot semitransparent, i.e. use alpha=.8 or similar.
  2. Use plot instead of scatter.
Monday, November 8, 2021
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :