Is there an alternative to axes.clear() that leaves the axis labels untouched, while erasing the axes' contents?
Context: I have an interactive script that flips through some flow images, and for each image, plots it using axes.quiver(). If I don't call axes.clear() between calls to axes.quiver(), each quiver() call just adds more arrows to the plot without first erasing the previously added arrows. However, when I call axes.clear(), it nukes the axes labels. I can re-set them, but it's a bit annoying.
Use plt. clf() to clear a plot Call plt. clf() to clear the current figure of plt .
pyplot. clf(). Used to clear the current Figure's state without closing it.
You can remove the artists from an axes using the remove()
of the artists. Below is a code showing two options to do so.
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.figure()
plt.title('Arrows scale with plot width, not view')
plt.xlabel('xlabel')
plt.xlabel('ylabel')
Q = plt.quiver(X, Y, U, V, units='width')
l, = plt.plot(X[0,:], U[4,:]+2)
# option 1, remove single artists
#Q.remove()
#l.remove()
# option 2, remove all lines and collections
for artist in plt.gca().lines + plt.gca().collections:
artist.remove()
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With