欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Cartopy0.20最新功能——Cartopy装不上别慌,内附解决方案

时间:2023-04-23

Cartopy 0.20 最新功能

背景介绍

Cartopy 是英国气象局开发的地图绘图包,实现了 basemap 的大部分功能,利用了强大的PROJ.4、NumPy和Shapely库,并在Matplotlib之上构建了一个编程接口,用于创建发布质量的地图,并进行地理空间数据处理与空间数据分析,对于地图学与地理信息系统、大气科学专业非常受用。

虽然,

Cartopy的安装依赖较为复杂,推荐使用conda install cartopy进行安装,但很多同学可能因为安装就从入门到放弃了qaq。低版本的Cartopy(0.18及以下)功能仍然比较有限,对于非等经纬度的投影不能进行标注等细节问题。

但是,

ModelWhale提供了气象数据分析镜像,免去了环境配置和模块安装的困扰,提供了常用的Python模块,不定期进行维护和更新最新基于Python3.9版本镜像中包含Cartopy 0.20模块,集成了很多新功能,并且更加稳定和友好
新功能介绍

网址:https://scitools.org.uk/cartopy/docs/latest/whatsnew/v0.20.html

新功能展示 1.支持六边形图(Hexbin)

fig = plt.figure(figsize=(10, 5))ax = plt.axes(projection=ccrs.Robinson())ax.coastlines()x, y = np.meshgrid(np.arange(-179, 181), np.arange(-90, 91))data = np.sqrt(x**2 + y**2)ax.hexbin(x.flatten(), y.flatten(), C=data.flatten(), gridsize=20, transform=ccrs.PlateCarree())plt.show()

2.引入了古德分瓣投影

fig = plt.figure(figsize=(10, 5))proj = ccrs.InterruptedGoodeHomolosine(central_longitude=-160,emphasis='ocean')ax = plt.axes(projection=proj)ax.stock_img()plt.show()

3.解决了不同投影下的经纬度标注问题

rotated_crs = ccrs.RotatedPole(pole_longitude=120.0, pole_latitude=70.0)ax0 = plt.axes(projection=rotated_crs)ax0.set_extent([-6, 1, 47.5, 51.5], crs=ccrs.PlateCarree())ax0.add_feature(cfeature.LAND.with_scale('110m'))ax0.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)plt.figure(figsize=(6.9228, 3))ax1 = plt.axes(projection=ccrs.InterruptedGoodeHomolosine())ax1.coastlines(resolution='110m')ax1.gridlines(draw_labels=True)plt.figure(figsize=(7, 3))ax2 = plt.axes(projection=ccrs.PlateCarree())ax2.coastlines(resolution='110m')gl = ax2.gridlines(draw_labels=True)gl.top_labels = Falsegl.right_labels = Falseplt.show()


4.对图像进行投影

fig = plt.figure(figsize=(8, 12))# !wget https://lance-modis.eosdis.nasa.gov/imagery/gallery/2012270-0926/Miriam.A2012270.2050.2km.jpgfname = '/home/mw/project/Miriam.A2012270.2050.2km.jpg'img_extent = (-120.67660000000001, -106.32104523100001, 13.2301484511245, 30.766899999999502)img = plt.imread(fname)ax = plt.axes(projection=ccrs.PlateCarree())ax.set_xmargin(0.05)ax.set_ymargin(0.10)ax.imshow(img, origin='upper', extent=img_extent, transform=ccrs.PlateCarree())ax.coastlines(resolution='50m', color='black', linewidth=1)ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)ax.plot(-117.1625, 32.715, 'bo', markersize=7, transform=ccrs.Geodetic())ax.text(-117, 33, 'San Diego', transform=ccrs.Geodetic())plt.show()

5.与xarray完美兼容

ds = xr.open_dataset('/home/mw/input/OISSTV27010/anom/sst.day.anom.2015.nc')ds

ssta = ds.anom.sel(time='2015-07-01', method='nearest')fig = plt.figure(figsize=(9,6))ax = plt.axes(projection=ccrs.Robinson())ax.coastlines()gl = ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)gl.top_labels = Falsegl.right_labels = Falsessta.plot(ax=ax, transform=ccrs.PlateCarree(), vmin=-5, vmax=5, cmap=cmaps.cmp_b2r, cbar_kwargs={'shrink': 0.4})plt.show()

6.绘制兰伯特投影的中国版图

标准地图服务:http://211.159.153.75/

标准地图依据中国和世界各国国界线画法标准编制而成,可用于新闻宣传用图、书刊报纸插图、广告展示背景图、工艺品设计底图等,也可作为编制公开版地图的参考底图。社会公众可以免费浏览、下载标准地图,直接使用标准地图时需要标注审图号。

# 设置投影proj = ccrs.LambertConformal(central_longitude=110, central_latitude=90,standard_parallels=(25, 47))# 创建图例fig = plt.figure(figsize=(10, 8),frameon=True)ax = fig.add_axes([0.08, 0.05, 0.8, 0.94], projection=proj)ax.set_extent([80, 130, 15, 55],crs=ccrs.PlateCarree())ax.tick_params(labelsize=15)# 添加基础地理图层ax.add_feature(cfeature.OCEAN.with_scale('50m'))ax.add_feature(cfeature.LAND.with_scale('50m'))ax.add_feature(cfeature.RIVERS.with_scale('50m'))ax.add_feature(cfeature.LAKES.with_scale('50m'))# 添加省边界和九段线province = shpreader.Reader('/home/mw/input/china_shp3798/province.shp')nineline = shpreader.Reader('/home/mw/input/china_shp3798/nine_line.shp')ax.add_geometries(province.geometries(), crs=ccrs.PlateCarree(), edgecolor='k',facecolor='none')ax.add_geometries(nineline.geometries(), crs=ccrs.PlateCarree(), color='#8B0000')# 标注经纬度gl = ax.gridlines(draw_labels=True, x_inline=False, y_inline=False, dms=True, xlocs=np.arange(50,170,10), ylocs=np.arange(0,60,10), linestyle='--', lw=1, rotate_labels=False, color='dimgrey', crs=ccrs.PlateCarree())gl.top_labels = Falsegl.right_labels = False# 添加南海附图sub_ax = fig.add_axes([0.75, 0.095, 0.15, 0.2], projection=ccrs.LambertConformal(central_latitude=90, central_longitude=110))# 添加基础地理图层sub_ax.add_feature(cfeature.OCEAN.with_scale('50m'))sub_ax.add_feature(cfeature.LAND.with_scale('50m'))sub_ax.add_feature(cfeature.RIVERS.with_scale('50m'))sub_ax.add_feature(cfeature.LAKES.with_scale('50m'))# 添加省边界和九段线sub_ax.set_extent([106, 120, 2, 24],crs=ccrs.PlateCarree())sub_ax.add_geometries(province.geometries(), crs=ccrs.PlateCarree(), edgecolor='k',facecolor='none')sub_ax.add_geometries(nineline.geometries(), crs=ccrs.PlateCarree(), color='#8B0000')plt.show()

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。