(二)Cartopy中添加地理要素(海岸线、陆地、河流等)

时间:2024-03-09 16:26:14

1、绘制海岸线、陆地、河流等要素

  我们在进行地图绘制时,除所属显示地图数据外,在地图空白区域,往往需要添加一些地理要素,如海岸线、陆地、河流等。一方面可以弥补空白区域造成的视觉上的不美观,另一方面也更符合制图规范。

  使用Cartopy进行绘制地理要素代码如下:

import matplotlib.pyplot as plt###引入库包
import cartopy.crs as ccrs

import cartopy.feature as cfeature
proj = ccrs.PlateCarree(central_longitude=130)##
fig = plt.figure(figsize=(3, 2), dpi=550)  # 创建页面
ax = fig.subplots(1, 1, subplot_kw={\'projection\': proj})  # 创建子图
ax.add_feature(cfeature.LAND)####添加陆地######
ax.add_feature(cfeature.COASTLINE,lw=0.25)#####添加海岸线#########
ax.add_feature(cfeature.RIVERS,lw=0.25)#####添加河流######
ax.add_feature(cfeature.LAKES)######添加湖泊#####
# ax.add_feature(cfeature.BORDERS, linestyle=\'-\',lw=0.25)####此方法用来绘制国外地图尚可,如绘制我国地图是不正确的,会丢失了藏南、*等领土############
ax.add_feature(cfeature.OCEAN)######添加海洋########
plt.show()

  其中,线状要素可以使用关键字lw设置线宽,linestyle设置线型。线状和面状要素可以使用color关键字设置颜色,其他的关键字参数可查阅matplotlib的pyplot模块。

2、添加经纬度信息

  当前地图没有添加经纬度信息,使用Cartopy增加经纬度信息代码如下所示:

import matplotlib.ticker as mticker
import numpy as np
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
dlon, dlat = 60, 30
xticks = np.arange(0, 360.1, dlon)
yticks = np.arange(-90, 90.1, dlat)
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False,
        linewidth=1, linestyle=\':\', color=\'k\', alpha=0.8)
gl.xlocator = mticker.FixedLocator(xticks)
gl.ylocator = mticker.FixedLocator(yticks)
ax.set_xticks(xticks, crs=ccrs.PlateCarree())
ax.set_yticks(yticks, crs=ccrs.PlateCarree())
ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=True))
ax.yaxis.set_major_formatter(LatitudeFormatter())
fig_fname = "全球地图.png"
plt.savefig(fig_fname, dpi=500, bbox_inches=\'tight\')

3、使用更高分辨率地理要素

  当绘制局部范围地图时,使用默认的添加地理要素,所绘制图像会出现失真现象,这是由于默认的地理要素分辨率不够导致的,默认的分辨率为110m,在绘制时可以设置要素分辨率,代码如下:

ax.add_feature(cfeature.LAND.with_scale(\'10m\'))####添加陆地######
ax.add_feature(cfeature.COASTLINE.with_scale(\'10m\'),lw=0.25)#####添加海岸线#########
ax.add_feature(cfeature.RIVERS.with_scale(\'10m\'),lw=0.25)#####添加河流######
ax.add_feature(cfeature.LAKES.with_scale(\'10m\'))######添加湖泊#####
# ax.add_feature(cfeature.BORDERS, linestyle=\'-\',lw=0.25)####不推荐,我国丢失了藏南、*等领土############
ax.add_feature(cfeature.OCEAN.with_scale(\'10m\'))######添加海洋########

 

  注意:使用这种方式绘图第一次绘图速度很慢,原因是所需数据需要联网下载,第二次后速度会快很多。

4、绘制地球阴影浮雕图

  Cartopy提供了地球阴影浮雕图的绘制,可使用此图作为空白地图的背景图,代码如下:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt

ax = plt.axes(projection=ccrs.Mollweide())
ax.stock_img()
plt.show()

 

 

 

5、绘制点、线、文字

   使用Cartopy绘制在地图上绘制点、线、文字的代码如下:

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

6、添加中国国界线

  在实际应用过程中,很多时候需要在地图上绘制中国国界线,Cartopy中提供的cfeature.BORDERS无法满足要求,会丢失了藏南、*等领土;因此需要读取中国的shapefile文件,增加国界线。代码如下:

import cartopy.feature as cfeature
from cartopy.io.shapereader import Reader
proj = ccrs.PlateCarree(central_longitude=130)##
fig = plt.figure(figsize=(6, 4), )  # 创建页面
ax = fig.subplots(1, 1, subplot_kw={\'projection\': proj})  # 创建子图
ax.set_extent([60,139,5,54])
ax.add_feature(cfeature.LAND.with_scale(\'10m\'))####添加陆地######
ax.add_feature(cfeature.COASTLINE.with_scale(\'10m\'),lw=0.25)#####添加海岸线#########
ax.add_feature(cfeature.RIVERS.with_scale(\'10m\'),lw=0.25)#####添加河流######
ax.add_feature(cfeature.LAKES.with_scale(\'10m\'))######添加湖泊#####
# ax.add_feature(cfeature.BORDERS, linestyle=\'-\',lw=0.25)####不推荐,我国丢失了藏南、*等领土############
ax.add_feature(cfeature.OCEAN.with_scale(\'10m\'))######添加海洋########

reader = Reader(r"china_country.shp") #国界
china_country= cfeature.ShapelyFeature(reader.geometries(), ccrs.PlateCarree(), edgecolor=\'black\', facecolor=\'none\') 
ax.add_feature(
china_country, linewidth=0.7) # 添加国界线
plt.show()

 

 

 

参考

微信公众号:云台书史:https://mp.weixin.qq.com/s/tY5Qjo1kIG4cCut3a6Lnzw

Cartopy官网地址:https://scitools.org.uk/cartopy/docs/latest/matplotlib/intro.html