How to remove contour colors outside the continental United States (inside Mexico and Canada) from the base map?

I have data for visualization on a map of the USA. I do not want any color on lands outside the United States (in Mexico in the southwest and in Canada in the northeast). How can I hide these areas from the outline? Please note that it doesn’t matter if these state borders are drawn or not.

The code:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, maskoceans
from scipy.interpolate import griddata
np.random.seed(77)



lllat = 24.396308
lllon = -124.848974
urlat =  49.384358
urlon = -66.885444
m = Basemap(llcrnrlat=lllat,
urcrnrlat=urlat,
llcrnrlon=lllon,
urcrnrlon=urlon,
resolution='i', projection='cyl')
m.drawcountries(linewidth=1.0)
m.drawstates(linewidth=1.0, color='lightgray')
m.drawlsmask(land_color='gray',ocean_color="#b0c4de", lakes=True)

#create 100 random latitudes
lats = np.random.randint(low=lllat-1, high=urlat+1, size=1000) + np.random.ranf(size=1000)
#create 100 random longitudes
lons = np.random.randint(low=lllon-1, high=urlon+1, size=1000) + np.random.ranf(size=1000)
#create 100 random values/probabilities
probabilities = np.random.random(size=1000)


#now use meshgrid and contourf to visualize it
mlon, mlat = m(*(lons, lats))
# grid data
numcols, numrows = 1000, 1000
xi = np.linspace(mlon.min(), mlon.max(), numcols)
yi = np.linspace(mlat.min(), mlat.max(), numrows)

xi, yi = np.meshgrid(xi, yi)
# interpolate
x, y, z = mlon, mlat, probabilities
zi = griddata((mlon, mlat), probabilities, (xi, yi), method='nearest', rescale=False)
data = maskoceans(xi, yi, zi)
con = m.contourf(xi, yi, data, cmap=plt.get_cmap('YlOrRd'))
cbar = m.colorbar(con,location='right',pad="3%")
plt.show()

which creates the following image enter image description here  Pay attention to colors outside the borders of the USA. I want them to be deleted.

I tried to remove points from lats and logs that do not exist in the US, but still the outline colors of some parts outside the border.

I could mask the waters with an ocean mask, but I could not mask these points in Mexico and Canada.

. zi nan , , , , .

+4
1

. . :

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, cm, maskoceans
from scipy.interpolate import griddata
from matplotlib.patches import Polygon as MplPolygon
import shapefile
import pdb
np.random.seed(77)



lllat = 24.396308
lllon = -124.848974
urlat =  49.384358
urlon = -66.885444
m = Basemap(llcrnrlat=lllat,
urcrnrlat=urlat,
llcrnrlon=lllon,
urcrnrlon=urlon,
resolution='i', projection='cyl')
m.drawcountries(linewidth=1.0)
m.drawstates(linewidth=1, color='lightgray')
m.drawcoastlines()
m.drawlsmask(land_color='gray',ocean_color="#b0c4de", lakes=True)

#create 100 random latitudes
lats = np.random.randint(low=lllat-1, high=urlat+1, size=1000) + np.random.ranf(size=1000)
#create 100 random longitudes
lons = np.random.randint(low=lllon-1, high=urlon+1, size=1000) + np.random.ranf(size=1000)
#create 100 random values/probabilities
probabilities = np.random.random(size=1000)


#now use meshgrid and contourf to visualize it
mlon, mlat = m(*(lons, lats))
# grid data
numcols, numrows = 1000, 1000
xi = np.linspace(mlon.min(), mlon.max(), numcols)
yi = np.linspace(mlat.min(), mlat.max(), numrows)

xi, yi = np.meshgrid(xi, yi)
# interpolate
x, y, z = mlon, mlat, probabilities
zi = griddata((mlon, mlat), probabilities, (xi, yi), method='nearest', rescale=False)
data = maskoceans(xi, yi, zi)
con = m.contourf(xi, yi, data, cmap=plt.get_cmap('YlOrRd'))
cbar = m.colorbar(con,location='right',pad="3%")

world_shp_info = m.readshapefile('./data/CNTR_2014_10M_SH/Data/CNTR_RG_10M_2014','world',drawbounds=False)

ax = plt.gca()
for shapedict,state in zip(m.world_info, m.world):
    if shapedict['CNTR_ID'] not in ['CA', 'MX']: continue
    poly = MplPolygon(state,facecolor='gray',edgecolor='gray')
    ax.add_patch(poly)
plt.show()

.

, .

+1

Source: https://habr.com/ru/post/1671949/


All Articles