本篇文章给大家分享的是有关python Cartopy的基础使用方法,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
常用地图底图的绘制一般由技术或者Cartopy模块完成,由于技术库是基于python2开发的一个模块,目前已经不开发维护。故简单介绍Cartopy模块的一些基础操作,一基础介绍
首先导入相关模块。
进口numpy np 进口matplotlib。pyplot作为plt 进口cartopy。crs的ccr自己 进口cartopy。作为cfeature 从cartopy.mpl。股票行情自动收录器进口LongitudeFormatter LatitudeFormatter
首先介绍参数投影,该命令可以配合ccr自己设置投影类型,此处以方形投影命令为示例。其中central_longitude参数为投影中心位置,其中心设置与基础图设置规则一样,详情可以看上一篇文章。
ax=plt.axes(投影=ccrs.PlateCarree (central_longitude=0))
在设置好绘制类型后,绘制地图各特征量。其代码如下:
# ax.add_feature (cfeature.LAKES.with_scale(规模)) ax.add_feature (cfeature.OCEAN.with_scale(规模) # ax.add_feature (cfeature.RIVERS.with_scale(规模)) # ax.add_feature (cfeature.LAND.with_scale(规模),lw=0.5) ax.add_feature (cfeature.COASTLINE.with_scale(规模),lw=2)
参数比例为地图分辨率,目前支持10米、50米、110米,参数lw为线条粗细。此处绘制海岸线和海洋,效果图如下:
ax.set_xticks (np.arange (0361, 40), crs=ccrs.PlateCarree ()) ax.set_yticks (np.arange (-90、90 + 30、30), crs=ccrs.PlateCarree ()) # zero_direction_label用来设置经度的0度加不加E和W lon_formatter=LongitudeFormatter (zero_direction_label=False) lat_formatter=LatitudeFormatter () ax.xaxis.set_major_formatter (lon_formatter) ax.yaxis.set_major_formatter (lat_formatter)
可以看到效果图如下:
ax.outline_patch.set_visible(假) ax.spines[& # 39;底部# 39;].set_visible(真正的) ax.spines[& # 39;左# 39;].set_visible(真正的) ax.spines[& # 39;对# 39;].set_visible(真正的) ax.spines[& # 39;顶部# 39;].set_visible(真正的) ax.spines[& # 39;底部# 39;].set_linewidth(2.5), # # #设置底部坐标轴的粗细 ax.spines[& # 39;左# 39;].set_linewidth(2.5), # # # #设置左边坐标轴的粗细 ax.spines[& # 39;对# 39;].set_linewidth(2.5), # # #设置右边坐标轴的粗细 ax.spines[& # 39;顶部# 39;].set_linewidth(2.5), # # # #设置上部坐标轴的粗细
应该在该模块下,控制坐标轴的命令已经和常规不一样,因此先关闭该控制,然后开启常规坐标轴设置。
当我们在某一小块区域研究时,需要绘制区域地图。此时我们可以引入命令:
ax.set_extent(盒子,crs=ccrs.PlateCarree ())
其中盒为绘制区域,crs为投影类型。其他命令基本不变。设置盒为(40180 0 90),可得到效果图如下:
为方便各位读者,我书写了绘制地图的函数,大家在使用时可直接调用。此处示例为方形投影,若希望绘制其他投影。只需要修改函数部分参数即可。代码如下:
def map_make(规模、盒子、xstep ystep): ax=plt.axes(投影=ccrs.PlateCarree (central_longitude=180) [1]=(框盒[0])//xstep x_start [1]——* xstep=框 [3]=(框盒[2])//ystep y_start [3]——* ystep=框 ax.set_extent(盒子,crs=ccrs.PlateCarree ()) # ax.add_feature (cfeature.LAKES.with_scale(规模)) # ax.add_feature (cfeature.OCEAN.with_scale(规模)) # ax.add_feature (cfeature.RIVERS.with_scale(规模)) # ax.add_feature (cfeature.LAND.with_scale(规模),lw=0.5) ax.add_feature (cfeature.COASTLINE.with_scale(规模),lw=2) ax.set_xticks (np.arange (x_start箱[1]+ xstep xstep), crs=ccrs.PlateCarree ()) ax.set_yticks (np.arange (y_start箱[3]+ ystep ystep), crs=ccrs.PlateCarree ()) # zero_direction_label用来设置经度的0度加不加E和W lon_formatter=LongitudeFormatter (zero_direction_label=False) lat_formatter=LatitudeFormatter () ax.xaxis.set_major_formatter (lon_formatter) ax.yaxis.set_major_formatter (lat_formatter) #添加网格线 ax.grid () ax.outline_patch.set_visible(假) ax.spines[& # 39;底部# 39;].set_visible(真正的) ax.spines[& # 39;左# 39;].set_visible(真正的) ax.spines[& # 39;对# 39;].set_visible(真正的) ax.spines[& # 39;顶部# 39;].set_visible(真正的) ax.spines[& # 39;底部# 39;].set_linewidth(2.5), # # #设置底部坐标轴的粗细 ax.spines[& # 39;左# 39;].set_linewidth(2.5), # # # #设置左边坐标轴的粗细 ax.spines[& # 39;对# 39;].set_linewidth(2.5), # # #设置右边坐标轴的粗细 ax.spines[& # 39;顶部# 39;].set_linewidth(2.5), # # # #设置上部坐标轴的粗细 返回axpython Cartopy的基础使用方法