直接使用cartopy库绘制中国以及周边国家的地图时,中国国界线有问题(藏南地区没有画到中国国界线内等)也没有九段线。下面的程序可以将国家标准国界线和九段线都添加到地图上,并标出国家名称
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.io.shapereader import Reader
from cartopy.feature.nightshade import Nightshade
from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter
def create_map(date_time_arg_time, bbox=None):
"""绘制中国和周边国家的地图,并标出主要国家的名称
Args:
date_time_arg_time: 朝地图上绘制黑夜所需的时间, python datetime object
bbox: 地图边界范围
Returns:
fig, ax 用于绘制风场的地图基底的图和轴
"""
if bbox is None:
bbox = [50, 170, -20, 60]
fig = plt.figure(figsize=(12.8, 10.8), tight_layout=True)
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
plt.rcParams['font.size'] = 14 # 绘图中默认字体大小
# bbox = [50, 170, -20, 60] # NH区域 [西边经度,东边经度,南边纬度,北边纬度]
ax.set_extent(bbox, crs=ccrs.PlateCarree())
ax.add_feature(Nightshade(date_time_arg_time, alpha=0.2)) # 夜色特征
ax.add_feature(cfeature.OCEAN) # Add ocean
ax.add_feature(cfeature.LAND) # Add land
# ax.add_feature(cfeature.RIVERS) # Add rivers
# ax.add_feature(cfeature.LAKES, alpha=0.5) # Add lakes
ax.add_feature(cfeature.BORDERS) # Add borders
# 绘制中国边界
shp_path = 'Geographic data/china-shapefiles/shapefiles/'
china_border = cfeature.ShapelyFeature(
Reader(shp_path + 'china_country.shp').geometries(),
ccrs.PlateCarree(),
edgecolor='k',
facecolor='none'
)
nine_dotted_line = cfeature.ShapelyFeature(
Reader(shp_path + 'china_nine_dotted_line.shp').geometries(),
ccrs.PlateCarree(),
edgecolor='k',
facecolor='none'
)
ax.add_feature(china_border, linewidth=2.0)
ax.add_feature(nine_dotted_line, linewidth=2.0)
# 标注主要国家
country_list = [(140.39, 38.09, '日本'),
(100.99, 15.87, '泰国'),
(102.50, 19.86, '老挝'),
(78.96, 20.59, '印度'),
(120.42, -4.88, '印度尼西亚'),
(103.82, 1.35, '新加坡'),
(95.96, 21.22, '缅甸'),
(104.75, 12.25, '柬埔寨'),
(89.97, 23.52, '孟加拉国'),
(70.30, 30.25, '巴基斯坦'),
(65.52, 33.81, '阿富汗'),
(55.11, 31.42, '伊朗'),
(57.81, 21.06, '阿曼'),
(58.00, 39.28, '土库曼斯坦'),
(64.03, 41.65, '乌兹别克\n斯坦'),
(69.79, 47.69, '哈萨克斯坦'),
(103.85, 46.86, '蒙古'), ]
text_params = {'ha': 'center', 'va': 'center', 'family': 'sans-serif',
'fontname': 'SimHei', }
ax.text(104.70, 34.86, '中华人民共和国', color='r', **text_params)
for i, j, k in country_list:
ax.text(i, j, k, fontsize=10, **text_params)
# 标注坐标轴
lon_step, lat_step = 10, 10
ax.set_xticks(np.arange(bbox[0], bbox[1] + lon_step, lon_step), crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(bbox[2], bbox[3] + lat_step, lat_step), crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter(zero_direction_label=False) # zero_direction_label用来设置经度的0度加不加E和W
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
plt.xticks(size=14)
plt.yticks(size=14)
ax.grid()
return fig, ax
该程序还可以显示白天黑夜,第一的输入参数date_time_arg_time
需要是 Python datetime 对象(例如 date_time_arg_time = datetime.datetime(2019,1,1))。还需要注意的是程序中使用了中国国界线和九段线(现在称为十段线)的矢量数据china_country.shp和china_nine_dotted_line.shp,这些数据可以到网站https://github.com/dongli/china-shapefiles下载。