In Matplotlib, how can I clear the content of the axes without erasing the axis labels?

Is there an alternative to axes.clear () that leaves the axis labels intact when erasing the content of the axes?

Context: I have an interactive script that loops through some streaming images and for each image, it uses it using axes.quiver (). If I don't call axes.clear () between calls to axes.quiver (), each call to quiver () simply adds more arrows to the graph without first removing the previously added arrows. However, when I call axes.clear () it clicks the axis labels. I can reinstall them, but this is a little annoying.


source to share

1 answer

You can remove artists from axes using remove()

artists. Below is the code showing two options for this.

import matplotlib.pyplot as plt
import numpy as np

X, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))
U = np.cos(X)
V = np.sin(Y)

plt.title('Arrows scale with plot width, not view')

Q = plt.quiver(X, Y, U, V, units='width')
l, = plt.plot(X[0,:], U[4,:]+2)

# option 1, remove single artists

# option 2, remove all lines and collections
for artist in plt.gca().lines + plt.gca().collections:




All Articles