python 基于shp文件绘制完整中国地图(matplotlib,cartopy)

思路:中国地图画两遍,截取响相应经纬度范围的区域

难点:中国海岸线以及南海岛屿等数据的准确性

解决思路:在阿里云上获取中国地图的json文件,离线转成shp文件(网上有教程,也可留言获取)

效果图:

python 基于shp文件绘制完整中国地图(matplotlib,cartopy)_第1张图片

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

#新建图层
fig = plt.figure(figsize=(15,15))
ax = fig.add_subplot(111,projection=ccrs.PlateCarree())

#绘制中国地图
shp_path = r'D:\ERA5\shp\中国\中国.shp'
reader = shapereader.Reader(shp_path)
for record in reader.records():
    ax.add_geometries([record.geometry],ccrs.PlateCarree(),facecolor='none')
ax.add_feature(cfeature.RIVERS.with_scale('50m'))# 添加河流,湖泊
ax.add_feature(cfeature.LAKES.with_scale('50m'))
ax.set_extent([73, 135, 13, 55])

#绘制南海子图
sub_ax = fig.add_axes([0.764, 0.22, 0.15, 0.15],
                      projection=ccrs.PlateCarree())
for record in reader.records():
    sub_ax.add_geometries([record.geometry],ccrs.PlateCarree(),facecolor='none')
sub_ax.set_extent([105,125,0,25], crs=ccrs.PlateCarree())

plt.show()

你可能感兴趣的:(python,matplotlib)