Keeping map overlay between plots in matplotlib
Asked Answered
L

1

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?

Lawanda answered 17/2, 2017 at 12:51 Comment(0)
B
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("...")
Booher answered 17/2, 2017 at 16:20 Comment(2)
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 iterableLawanda
Don't use a comma. scat = plt.scatter(...)Tendency

© 2022 - 2024 — McMap. All rights reserved.