1

I am creating a series of lat/long scatterplots on a map from basemap. I am planning on creating thousands of plot, with different lat/long data. To save time I wanted to draw the map overlay only once:

map = Basemap(epsg=3395, projection='merc', lat_0=59.45, lon_0=10.5,
resolution = 'h',
llcrnrlon=minlong, llcrnrlat=minlat,
urcrnrlon=maxlong, urcrnrlat=maxlat
map.arcgisimage(service='ESRI_Imagery_World_2D', xpixels=3000, verbose=True)

However, I find no way of clearing my previous scatterplot before plotting a new one.

for each set in sets:
   x = set[0]
   y = set[1]
   x,y = map(x,y)
   plt.scatter(x,y, s=2.5, alpha=1, color=c, edgecolors='none')
   plt.savefig('title.png', format='png', bbox_inches='tight', dpi=500)

If I do the following:

plt.clf()

or

plt.close()

I have to redraw my map. If I don't have anything, the scatter plot from the previous iteration are plotted. So how do I remove all scatterplot data, but keeping the map data?

1 Answer 1

1

What about just changing the data of your scatter plot?

# save an empty scatter plot
scat = plt.scatter([], [],  s=2.5, alpha=1, color=c, edgecolors='none')
for each set in sets:
    # The data needs to be written as [(x1, y1), (x2, y2), ...]
    scat.set_offsets([xy for xy in zip(x, y)])
    plt.savefig("...")
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, when implementing your suggestion I get the following error: scat, = plt.scatter([],[], s=2.5, alpha=1, color=c, edgecolors='none') TypeError: 'PathCollection' object is not iterable
Don't use a comma. scat = plt.scatter(...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.