Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw Scatter plot on top of background using Basemap

I am trying to plot a scatter plot on a background using basemap. But it's overwriting the background. How do I retain the background?

I am using this code

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

m = Basemap(projection='merc',llcrnrlat=-80,urcrnrlat=80,llcrnrlon=-180,urcrnrlon=180,lat_ts=20,resolution='c')

m.bluemarble()

x, y = m(list(longitude), list(latitude))
plt.scatter(x,y,1,marker='o',color='Red')
plt.show()

But as soon as I run the scatter plot, its overwriting background image. How can I overlay the scatter plot on the image.

like image 676
muni Avatar asked Nov 19 '25 07:11

muni


1 Answers

This is how to plot a series of points on top of a raster map. Note that the bluemarble image is huge, so a full scale (1.0 or default) plot of it should be avoided. The code is based on yours.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

# make up some data for scatter plot
lats = np.random.randint(-75, 75, size=20)
lons = np.random.randint(-179, 179, size=20)

fig = plt.gcf()
fig.set_size_inches(8, 6.5)

m = Basemap(projection='merc', \
            llcrnrlat=-80, urcrnrlat=80, \
            llcrnrlon=-180, urcrnrlon=180, \
            lat_ts=20, \
            resolution='c')

m.bluemarble(scale=0.2)   # full scale will be overkill
m.drawcoastlines(color='white', linewidth=0.2)  # add coastlines

x, y = m(lons, lats)  # transform coordinates
plt.scatter(x, y, 10, marker='o', color='Red') 

plt.show()

The resulting plot:

enter image description here

like image 196
swatchai Avatar answered Nov 20 '25 20:11

swatchai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!