Python: basic use of Cartopy


Preface

The drawing of the base map of commonly used maps is generally completed by the Basemap or cartopy module. Since the Basemap library is a module developed based on python2, it is currently not developed and maintained. Therefore, some basic operations of the cartopy module are briefly introduced.

1. Basic introduction

First import the relevant modules.

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

First introduce the parameter projection, this command can cooperate with ccrs to set the projection type, here we take the square projection command as an example. The central_longitude parameter is the center position of the projection. The central setting is the same as the Basemap setting rules. For details, please see the previous article.

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

After setting the drawing type, draw each feature quantity of the map. The code is as follows:

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

The parameter scale is the map resolution, currently supports 10m, 50m, 110m, and the parameter lw is the line thickness. The coastline and ocean are drawn here, and the renderings are as follows:
Insert picture description here
After drawing, it will be used as a map. Latitude and longitude are naturally indispensable. In this module, set the axis label at the same time to change the label scale. The specific form is as follows:

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)

You can see the effect diagram as follows:
Insert picture description here
Of course, if you want to change the thickness of the coordinate axis, you can introduce a command.

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);####设置上部坐标轴的粗细
   

It should be under this module, the command to control the coordinate axis is different from the conventional one. So turn off the control first, and then turn on the general axis settings.

2. Drawing of regional maps

When we study in a small area, we need to draw an area map. At this point we can introduce the command:

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

The box is the drawing area and crs is the projection type. Other commands remain basically unchanged. Set the box to [40,180,0,90], the effect picture can be obtained as follows:
Insert picture description here

to sum up

For the convenience of readers, I have written a function for drawing a map, which you can call directly when you use it. The example here is a square projection, if you want to draw other projections. Only need to modify some parameters of the function. code show as below:

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

Guess you like

Origin blog.csdn.net/weixin_49284189/article/details/109376547