5.1 Cartopy简介
5.2 多种投影介绍
5.3 地图信息添加
5.3.1 地理特征添加
5.3.2 网格和刻度线设置
5.4 填色图结合地图绘制
Cartopy是开源免费的Python扩展包,致力于用最简单直观的方式生成地图。
主要功能:
Cartopy除了跟Matplotlib有良好的接口外,跟Xarray也可以完美兼容,这样我们在使用Xarray的函数绘制图像的时候也可以非常方便地添加地理信息。
#Cartopy和Matplotlib的连接
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
fig = plt.figure()
ax = fig.add_subplot() #ax类型是axesSubplot
proj = ccrs.PlateCarree()
fig = plt.figure()
ax = fig.add_subplot(projection=proj) #ax类型是GeoaxesSubplot
ax.coastlines() #画海岸线
ax.gridlines() #画网格线
ax.stock_img() #叠加地形背景
#Cartopy和Xarray
import xarray as xr
ds = xr.open_dataset('filename.nc')
t = ds['t2m']-273.15
t.isel(time=0).plot()
proj = ccrs.PlateCarree()
fig = plt.figure()
ax = fig.add_subplot(projection=proj)
ax.coastlines()
t.isel(time=0).plot(ax=ax)
crs即坐标参考系统(Coordinate Reference Systems)之意
https://scitools.org.uk/cartopy/docs/v0.15/crs/projections.hmtl
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
projections = [
ccrs.PlateCarree(central_longitude=130.0),
ccrs.Mercator(central_longitude=130.0),
ccrs.LambertConformal(central_longitude=130.0),
ccrs.Orthographic(central_longitude=130.0) ]
for proj in projections:
fig = plt.figure()
ax = fig.add_subplot(projection=proj)
ax.coastlines()
ax.stock_img()
ax.set_title(f'{type(proj)}')
ATT! Cartopy自带的中国地图不符合我国的地图标准
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.feature as cfeat
fig = plt.figure()
proj = ccrs.PlateCarree()
ax = fig.add_subplot(projection=proj)
ax.coastlines()
ax.add_feature(cfeat.RIVERS.with_scale('110m'))
ax.add_feature(cfeat.LAKES.with_scale('110m'))
ax.add_feature(cfeat.LAND.with_scale('110m'))
ax.add_feature(cfeat.OCEAN.with_scale('110m'))
#黄河流域没显示
#三种分辨率 10m 50m 110m
for res in ['110m','50m','10m']: #50m效果比较好
proj = ccrs.PlateCarree()
ax = fig.add_subplot(projection=proj)
ax.coastlines()
ax.add_feature(cfeat.RIVERS.with_scale(res))
ax.add_feature(cfeat.LAKES.with_scale(res))
ax.add_feature(cfeat.OCEAN.with_scale(res))
ax.add_feature(cfeat.LAND.with_scale(res))
ax.set_title(f'{res}')
ax.set_extent([90,180,10,60])
fig = plt.figure()
proj = ccrs.PlateCarree()
ax = fig.add_subplot(projection=proj)
ax.coastlines()
ax.add_feature(cfeat.RIVERS.with_scale('110m'))
ax.add_feature(cfeat.LAKES.with_scale('110m'))
ax.add_feature(cfeat.LAND.with_scale('110m'))
ax.add_feature(cfeat.OCEAN.with_scale('110m'))
gl = ax.gridlines(
linewidth=0.9,
color='k',
linestyle='--',
alpha=0.5,
draw_labels=False )
#gl.top_labels = gl.right_labels = False #刻度显示不完整
import numpy as np
ax.set_xticks(np.arange(-180,181,30))
ax.set_yticks(np.arange(-90,91,30)) #刻度完整但是不标注NSWE,而且网格不是30°间隔
from cartopy.mpl.ticker import LongitudeFormatter,LatitudeFormatter
ax.xaxis.set_major_formatter(LongitudeFormatter)
ax.yaxis.set_major_formatter(LatitudeFormatter)
ax.xaxis.set_minor_locator(plt.MultipleLocator(10))
ax.yaxis.set_minor_locator(plt.MultipleLocator(10))
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeat
ds = xr.open_dataset('filename.nc')
t = ds['t2m']-273.15
fig = plt.figure(figsize = (12,12))
proj = ccrs.PlateCarree() #这个是底图投影
ax = fig.add_subplot(projection=proj)
# --- 设置特征
country = cfeat.ShapelyFeature(
gpd.read_file('filename.shp')['geometry'],
proj,
edgecolor = 'k',
facecoloe = None
)
ax.add_feature(country)
province = cfeat.ShapelyFeature(
gpd.read_file('filename.shp')['geometry'],
proj,
edgecolor = 'k',
facecoloe = None
)
ax.add_feature(province)
ax.add_feature(cfeat.RIVERS)
ax.add_feature(cfeat.OCEAN)
ax.add_feature(cfeat.LAKES)
# --- 设置经纬网格
gl = ax.gridlines(
linewidth=0.9,
color='k',
linestyle='--',
alpha=0.5,
draw_labels=False )
#gl.top_labels = gl.right_labels = False #刻度显示不完整
import numpy as np
ax.set_xticks(np.arange(-180,181,5))
ax.set_yticks(np.arange(-90,91,5)) #刻度完整但是不标注NSWE,而且网格不是30°间隔
from cartopy.mpl.ticker import LongitudeFormatter,LatitudeFormatter
ax.xaxis.set_major_formatter(LongitudeFormatter)
ax.yaxis.set_major_formatter(LatitudeFormatter)
ax.xaxis.set_minor_locator(plt.MultipleLocator(1))
ax.yaxis.set_minor_locator(plt.MultipleLocator(1))
# --- 设置区域
ax.set_extent([100,140,15,50],crs=proj)
# --- 可视化
cf = t.isel(time=0).plot(
ax=ax,
transform=ccrs.PlateCarree(), #transform是数据的坐标系统
add_colorbar=False )
# --- 设置色标
fig.colorbar(cf,ax=ax,orientation='horizontal',pad=0.06,aspect=50)
import geopandas as gpd
china = gdp.read_file('filename.shp')
#修改在最上边
# --- 设置南海子图
ax1 = fig.add_axes(
[0.657,0.284,0.2,0.24],
projection = proj )
country = cfeat.ShapelyFeature(
gpd.read_file('filename.shp')['geometry'],
proj,
edgecolor = 'k',
facecoloe = None
)
ax1.add_feature(country)
province = cfeat.ShapelyFeature(
gpd.read_file('filename.shp')['geometry'],
proj,
edgecolor = 'k',
facecoloe = None
)
ax1.add_feature(province)
ax1.set_extent([105,125,0,25],crs=proj)
cf = t.isel(time=0).plot(
ax=ax1,
transform=ccrs.PlateCarree(), #transform是数据的坐标系统
add_colorbar=False )
ax1.set_title('')