Cartopy Daily - 01
创建地图画布:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.PlateCarree())
其中的cartopy.crs为Coordinate Reference System,创建地图图层时选择参数projection
为地图设置所需投影,各类投影类型见API Reference。
投影常用类型有:
- ccrs.PlateCarree() 圆柱投影
- ccrs.LambertConformal() 兰伯特正形投影
- ccrs.Mercator() 墨卡托投影
- ccrs.Orthographic() 正交投影
- ccrs.Geostationary() 同步轨道投影
- ccrs.NearsidePerspective() 近处透视投影
投影与转换
创建地图时需要使用参数projection
,标注数据时将数据坐标按照投影类型转换,使数据画在正确的位置,使用transform
。
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
ny_lon, ny_lat = -75, 43
delhi_lon, delhi_lat = 77.23, 28.61
plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
color='blue', linewidth=2, marker='o',
transform=ccrs.Geodetic(),
)
plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat],
color='gray', linestyle='--',
transform=ccrs.PlateCarree(),
)
plt.text(ny_lon - 3, ny_lat - 12, 'New York',
horizontalalignment='right',
transform=ccrs.Geodetic())
plt.text(delhi_lon + 3, delhi_lat - 12, 'Delhi',
horizontalalignment='left',
transform=ccrs.Geodetic())
plt.show()
img1
其中,transform中的ccrs.Geodetic()
为球形拓扑转换,即如使用plot
画两点间连线,使用此转换得到的是大圆,而ccrs.PlateCarree()
得到的是正交经纬网下的直线连线再按照投影转变。
要使用经纬网格的二维数据绘制,在不同投影下都是用ccrs.PlateCarree()
来确保与投影相符。
经纬网格
经纬网绘制例子:Cartopy map gridlines and tick labels
绘制经纬网使用方法gridlines(),所有参数均为可选:
- crs: 网格的坐标系统,默认crs.PlanteCarree(),一般不同投影下不用改
draw_labels:
- string: "x" or "y"
- list: ["x", "y", "top", "bottom", "left", "right", "geo"]
- dict: {"bottom": "x", "left": "y"}
- xlim: (min, max)
- ylim: (min, max)
- ylocs: 经线位置,可传入数组
- xlocs: 纬线位置
与matplotlib的线类型属性通用:
- color: 线颜色
- linestyle: 线样式,见Linestyles,虚线可用
(0, (a, b))
,其中a,b为整数,a为虚线划线长度,b为虚线间隔长度。 - linewidth
设置仅在左边与底边显示经纬标签:
gl = ax.gridlines(draw_labels=True, ylocs=[20, 22, 24, 26], color="#444444", linestyle=(0, (1, 3)))
gl.top_labels = False
gl.right_labels = False
gl.xlabel_style = {'size': 15, 'color': 'gray'}
gl.ylabel_style = {'size': 12, 'family': "Times New Roman"}
设置地图范围
ax.set_extend([minLon, maxLon, minLat, maxLat])