我有一个数据要在美国地图上可视化我不想在美国以外的土地上(墨西哥西南部和东北部的加拿大)有任何颜色。如何从轮廓线中屏蔽这些区域?请注意,这些国家边界是否划定并不重要。
代码是:
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()
产生以下图像
注意美国边界外的颜色,我想把它们去掉。
我试着从美国中不存在的LATS和Lon去除点,但是仍然在边界之外的部分颜色。
我可以用海洋面具掩盖水,但不能掩盖墨西哥和加拿大的那些点。
注意:有一个解决方案可以循环所有网格点,并将所有不在美国的点的zi设置为nan,但考虑到我的实际数据大小,这对我来说是非常昂贵的计算。
最佳答案
我最终在轮廓线上画了墨西哥和加拿大多边形。已从here下载国家形状文件。代码如下:
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()
这就是结果。
我认为应该有一个更聪明的方法。
关于python - 如何从 basemap 中移除美国大陆以外(墨西哥和加拿大之内)的轮廓颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42708686/