入门

入门

使用 Cartopy 绘制区域地图

绘制亚洲地区的地形图(Miller 投影),并根据中国地区的 shapefile 绘制各个省份自治区的边界。

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.io.shapereader import Reader

Read shapefiles for provinces of China

shp = Reader('CHN_adm/CHN_adm1')

define the range of the area of interest

asia_west = 70
asia_east = 135
asia_south = 15
asia_north = 55

calculate the central longitude of the area

centrallongitude = (asia_west+asia_east)/2

fig = plt.figure()

set the projection to Miller

proj = ccrs.Miller(central_longitude = centrallongitude)
ax = fig.add_subplot(1, 1, 1,projection = proj)

set the range of the area of interest

img_extent = [asia_west, asia_east, asia_south, asia_north]
ax.set_extent(img_extent,crs = ccrs.PlateCarree())

set the background image to the 1cm:500km raster

fname = 'HYP_50M_SR_W/HYP_50M_SR_W.tif'
ax.imshow(plt.imread(fname), origin='upper', transform=ccrs.PlateCarree(),extent=[-180, 180, -90, 90])

set the gridlines to dashed line and the transparency to 0.7

ax.gridlines(linestyle='--',alpha=0.7)

add borders, coastline, rivers, lakes, and provinces of China

ax.add_feature(cfeature.BORDERS.with_scale('10m')) # high resolution
ax.add_feature(cfeature.COASTLINE.with_scale('50m')) # mediate resolution
ax.add_feature(cfeature.RIVERS) # low resolution
ax.add_feature(cfeature.LAKES) # low resolution
ax.add_geometries(shp.geometries(), crs=ccrs.PlateCarree(),edgecolor='k', linewidths=0.5,facecolor='none')
plt.show()

image.png

你可能感兴趣的:(入门)