栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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()

结果看起来像这样:



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/661674.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号