在底图中填充海洋[重复]

在底图中填充海洋[重复],第1张

在底图中填充海洋[重复]

我发现了一个更好的解决方案,即使用地图中海岸线定义的多边形来产生

matplotlib.PathPatch
覆盖海洋区域的多边形。该解决方案具有更好的分辨率,并且速度更快:

from matplotlib import pyplot as pltfrom mpl_toolkits import basemap as bmfrom matplotlib import colorsimport numpy as npimport numpy.ma as mafrom matplotlib.patches import Path, PathPatchfig, ax = plt.subplots()lon_0 = 319lat_0 = 72##some fake datalons = 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 mappolys = [p.boundary for p in m.landpolygons]##combining with map edgespolys = [map_edges]+polys[:]##creating a PathPatchpres = [    [Path.MOVETO] + [Path.LINETO for p in p[1:]]    for p in polys]polys_lin = [v for p in polys for v in p]pres_lin = [c for cs in pres for c in cs]path = Path(polys_lin, pres_lin)patch = PathPatch(path,facecolor='white', lw=0)##masking the data:ax.add_patch(patch)plt.show()

输出如下:

原始解决方案

您可以在中使用分辨率更高的数组

basemap.maskoceans
,以使分辨率适合大陆轮廓。然后,您只需反转蒙版并将蒙版数组绘制在数据之上。

不知何故,我只有

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

from matplotlib import pyplot as pltfrom mpl_toolkits import basemap as bmfrom matplotlib import colorsimport numpy as npimport numpy.ma as mafig, ax = plt.subplots()lon_0 = 319lat_0 = 72##some fake datalons = 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 limitslons2 = 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 maskcmap = colors.ListedColormap(['w'])pcol = ax.pcolormesh(x2,y2,masked, cmap=cmap)plt.show()

结果看起来像这样:



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5663631.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存