零基础小白python入门必看之Cartopy的基础使用

文章目录

  • 前言
  • 一、基础介绍
  • 二、区域地图的绘制
  • 总结

前言

常用地图底图的绘制一般由Basemap或者cartopy模块完成,由于Basemap库是基于python2开发的一个模块,目前已经不开发维护。故简单介绍cartopy模块的一些基础操作。

 

如果大家在学习中遇到困难,想找一个python学习交流环境,可以加入我们的python圈,裙号930900780,可领取python学习资料,会节约很多时间,减少很多遇到的难题。

一、基础介绍

首先导入相关模块。

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
12345

首先介绍参数projection,该命令可以配合ccrs设置投影类型,此处以方形投影命令为示例。其中central_longitude参数为投影中心位置。其中心设置与Basemap设置规则一样,详情可以看上一篇文章。

ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=0))
1

在设置好绘制类型后,绘制地图各特征量。其代码如下:

#ax.add_feature(cfeature.LAKES.with_scale(scale))
ax.add_feature(cfeature.OCEAN.with_scale(scale))
#ax.add_feature(cfeature.RIVERS.with_scale(scale))
#ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5)
ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2)
12345

参数scale为地图分辨率,目前支持10m,50m,110m,参数lw为线条粗细。此处绘制海岸线和海洋,效果图如下:

 


在绘制结束后,作为地图。经纬度自然是必不可少的,在该模块中,引进同时设置坐标轴标签改变该标签刻度的表示,具体形式如下:

ax.set_xticks(np.arange(0,361,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)
1234567

可以看到效果图如下:

 


当然如果想对坐标轴粗细变化可以引入一下命令。

ax.outline_patch.set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.spines['left'].set_visible(True)
ax.spines['right'].set_visible(True)
ax.spines['top'].set_visible(True)
ax.spines['bottom'].set_linewidth(2.5);###设置底部坐标轴的粗细
ax.spines['left'].set_linewidth(2.5);####设置左边坐标轴的粗细
ax.spines['right'].set_linewidth(2.5);###设置右边坐标轴的粗细
ax.spines['top'].set_linewidth(2.5);####设置上部坐标轴的粗细
   
12345678910

应该在该模块下,控制坐标轴的命令已经和常规不一样。因此先关闭该控制,然后开启常规坐标轴设置。

二、区域地图的绘制

当我们在某一小块区域研究时,需要绘制区域地图。此时我们可以引入命令:

ax.set_extent(box,crs=ccrs.PlateCarree())
1

其中box为绘制区域,crs为投影类型。其他命令基本不变。设置box为[40,180,0,90],可得到效果图如下:

 

总结

为方便各位读者,我书写了绘制地图的函数,大家在使用时可直接调用。此处示例为方形投影,若希望绘制其他投影。只需要修改函数部分参数即可。代码如下:

def map_make(scale,box,xstep,ystep):
    ax=plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
    a = (box[1]-box[0])//xstep
    x_start = box[1] - a*xstep
    a = (box[3]-box[2])//ystep
    y_start = box[3] - a*ystep
    ax.set_extent(box,crs=ccrs.PlateCarree())
    #ax.add_feature(cfeature.LAKES.with_scale(scale))
    #ax.add_feature(cfeature.OCEAN.with_scale(scale))
    #ax.add_feature(cfeature.RIVERS.with_scale(scale))
    #ax.add_feature(cfeature.LAND.with_scale(scale),lw=0.5)
    ax.add_feature(cfeature.COASTLINE.with_scale(scale),lw=2)
    
    ax.set_xticks(np.arange(x_start,box[1]+xstep,xstep), crs=ccrs.PlateCarree())
    ax.set_yticks(np.arange(y_start,box[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(False)
    ax.spines['bottom'].set_visible(True)
    ax.spines['left'].set_visible(True)
    ax.spines['right'].set_visible(True)
    ax.spines['top'].set_visible(True)
    ax.spines['bottom'].set_linewidth(2.5);###设置底部坐标轴的粗细
    ax.spines['left'].set_linewidth(2.5);####设置左边坐标轴的粗细
    ax.spines['right'].set_linewidth(2.5);###设置右边坐标轴的粗细
    ax.spines['top'].set_linewidth(2.5);####设置上部坐标轴的粗细
    
    return ax

最后多说一句,想学习Python可联系小编,这里有我自己整理的整套python学习资料和路线,想要这些资料的都可以进q裙930900780领取。

本文章素材来源于网络,如有侵权请联系删除。

posted @ 2020-11-02 17:04  python阿喵  阅读(2367)  评论(0编辑  收藏  举报