Reputation: 561
Let's say I make a series of AnnotationBbox'es in matplotlib through a for loop, like this:
for city in cities:
x, y = self.map(city[1], city[0])
ab = AnnotationBbox(imagebox, [x,y],
xybox=(0, 0),
xycoords='data',
boxcoords="offset points",
frameon=False)
self.axes.add_artist(ab)
self.locationImages.append(ab)
In this example, I've created a series of AnnotationBBoxes, and stored them in a list called self.locationImages. Then I go through the self.locationImages in a loop, and remove each one by doing this:
for image in self.locationImages:
image.remove()
Is there a way to remove all the artists, without having to go through a loop? Or to remove all artists, and lines completely, without having to remove the axes or the figure?
I'm plotting points on a map, and I need the map to stay. I'm doing zoom ins and outs, but during zoom ins and outs, I need to remove everything and replot. I'm working with a large data set and doing iterations is an expensive action
Upvotes: 3
Views: 5028
Reputation: 18284
I had multiple add_artist
earlier so removing just the 0th as suggested by @hayk didn't suffice for me . I did this:
if ax.artists and len(ax.artists) > 0:
for a in ax.artists:
a.remove()
Upvotes: 0
Reputation: 468
I believe a pretty nasty solution would be to do the following:
# remove all artists
while ax.artists != []:
ax.artists[0].remove()
#remove all lines
while ax.lines != []:
ax.lines[0].remove()
This works for dynamic plots (e.g. animation or ipywidgets
). But if you tell there has to be a better way -- I'd definitely agree :)
Upvotes: 1
Reputation: 68186
From matplotlib's pyplot
interface, you can pyplot.cla()
to clear an entire axis and pyplot.clf()
to clear an entire figure.
Upvotes: -1