在底图中填充海洋

Fill oceans in basemap

我正在尝试在 matplotlib.Basemap 上绘制 1x1 度数据,我想用白色填充海洋。但是,为了让海洋的边界遵循matplotlib绘制的海岸线,白色海洋遮罩的分辨率应该比我的数据分辨率高得多。

找了半天我尝试了两种可能的解决方案:

(1) maskoceans()is_land() 函数,但由于我的数据分辨率低于底图绘制的地图,因此边缘看起来不太好。我也不想将我的数据插入到更高分辨率。

(2) m.drawlsmask(),但由于无法分配 zorder,因此 pcolormesh 图始终覆盖掩码。

这个代码

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.basemap as bm

#Make data
lon = np.arange(0,360,1)
lat = np.arange(-90,91,1)
data = np.random.rand(len(lat),len(lon))

#Draw map
plt.figure()
m = bm.Basemap(resolution='i',projection='laea', width=1500000, height=2900000, lat_ts=60, lat_0=72, lon_0=319)
m.drawcoastlines(linewidth=1, color='white')
data, lon = bm.addcyclic(data,lon)
x,y = m(*np.meshgrid(lon,lat))
plt.pcolormesh(x,y,data)
plt.savefig('1.png',dpi=300)

生成此图像:

添加 m.fillcontinents(color='white') 生成以下图像,这是我需要的,但要填充海洋而不是陆地。

编辑:

m.drawmapboundary(fill_color='lightblue') 也填满了土地,因此不能使用。

期望的结果是海洋是白色的,而我用 plt.pcolormesh(x,y,data) 绘制的内容显示在陆地上。

我找到了一个更好的解决方案,它使用地图中海岸线定义的多边形来生成覆盖海洋区域的 matplotlib.PathPatch。此解决方案具有更好的分辨率并且速度更快:

from matplotlib import pyplot as plt
from mpl_toolkits import basemap as bm
from matplotlib import colors
import numpy as np
import numpy.ma as ma
from matplotlib.patches import Path, PathPatch

fig, ax = plt.subplots()

lon_0 = 319
lat_0 = 72

##some fake data
lons = np.linspace(lon_0-60,lon_0+60,10)
lats = np.linspace(lat_0-15,lat_0+15,5)
lon, lat = np.meshgrid(lons,lats)
TOPO = np.sin(np.pi*lon/180)*np.exp(lat/90)

m = bm.Basemap(resolution='i',projection='laea', width=1500000, height=2900000, lat_ts=60, lat_0=lat_0, lon_0=lon_0, ax = ax)
m.drawcoastlines(linewidth=0.5)

x,y = m(lon,lat)
pcol = ax.pcolormesh(x,y,TOPO)

##getting the limits of the map:
x0,x1 = ax.get_xlim()
y0,y1 = ax.get_ylim()
map_edges = np.array([[x0,y0],[x1,y0],[x1,y1],[x0,y1]])

##getting all polygons used to draw the coastlines of the map
polys = [p.boundary for p in m.landpolygons]

##combining with map edges
polys = [map_edges]+polys[:]

##creating a PathPatch
codes = [
    [Path.MOVETO] + [Path.LINETO for p in p[1:]]
    for p in polys
]
polys_lin = [v for p in polys for v in p]
codes_lin = [c for cs in codes for c in cs]
path = Path(polys_lin, codes_lin)
patch = PathPatch(path,facecolor='white', lw=0)

##masking the data:
ax.add_patch(patch)

plt.show()

输出如下所示:

原解:

您可以在 basemap.maskoceans 中使用更高分辨率的阵列,这样分辨率就适合大陆轮廓。之后,您只需反转掩码并在数据之上绘制掩码数组。

不知何故,当我使用地图的整个范围(例如,经度从 -180 到 180,纬度从 -90 到 90)时,我只得到 basemap.maskoceans 工作。鉴于需要相当高的分辨率才能使其看起来不错,因此计算需要一段时间:

from matplotlib import pyplot as plt
from mpl_toolkits import basemap as bm
from matplotlib import colors
import numpy as np
import numpy.ma as ma

fig, ax = plt.subplots()


lon_0 = 319
lat_0 = 72

##some fake data
lons = np.linspace(lon_0-60,lon_0+60,10)
lats = np.linspace(lat_0-15,lat_0+15,5)
lon, lat = np.meshgrid(lons,lats)
TOPO = np.sin(np.pi*lon/180)*np.exp(lat/90)


m = bm.Basemap(resolution='i',projection='laea', width=1500000, height=2900000, lat_ts=60, lat_0=lat_0, lon_0=lon_0, ax = ax)
m.drawcoastlines(linewidth=0.5)

x,y = m(lon,lat)

pcol = ax.pcolormesh(x,y,TOPO)


##producing a mask -- seems to only work with full coordinate limits
lons2 = np.linspace(-180,180,10000)
lats2 = np.linspace(-90,90,5000)
lon2, lat2 = np.meshgrid(lons2,lats2)
x2,y2 = m(lon2,lat2)
pseudo_data = np.ones_like(lon2)
masked = bm.maskoceans(lon2,lat2,pseudo_data)
masked.mask = ~masked.mask

##plotting the mask
cmap = colors.ListedColormap(['w'])
pcol = ax.pcolormesh(x2,y2,masked, cmap=cmap)

plt.show()

结果如下所示: