Keeping map overlay between plots in matplotlib

随声附和 提交于 2019-12-23 02:22:26

问题


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:


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("...")


来源:https://stackoverflow.com/questions/42298469/keeping-map-overlay-between-plots-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!