美文网首页
Python气象绘图(三):中国地图

Python气象绘图(三):中国地图

作者: 李HQ_1205 | 来源:发表于2020-01-14 15:35 被阅读0次

前言

这篇源自在网上看到一张绘制政治正确中国地图的帖子,下载了相应的数据和py脚本,运行时总是报错,遂逐条检查修改之后,得到正确的脚本。

原帖链接

原帖
https://cloud.tencent.com/developer/article/1484356

修改后的Python脚本

import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeat
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from cartopy.io.shapereader import Reader
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import warnings
import os
from cv2  import imread, imshow

def create_map():
    extent = [70, 140, 0, 60]
    shp_path = '/mnt/c/Users/LEEHQ/Downloads/chinamap/'
    # --创建画图空间
    proj = ccrs.PlateCarree()  # 创建坐标系
    fig = plt.figure(figsize=(6, 8), dpi=350)  # 创建页面
    ax = fig.subplots(1, 1, subplot_kw={'projection': proj})  # 创建子图
    # --设置地图属性
    reader = Reader(shp_path  +  'Province_9/Province_9.shp')
    provinces = cfeat.ShapelyFeature(reader.geometries(), proj, edgecolor='k', facecolor='none')
    ax.add_feature(provinces, linewidth=1)
    ax.set_extent(extent, crs=proj)
    # --增加低分辨率地形图(cartopy自带)
    #ax.stock_img()  # 增加地形图
    # --增加高分辨率地形图(需自行下载)
    #fname = os.path.join(config["repo_data_dir"], 'raster', 'natural_earth', '10-natural-earth-1.tif')
    fname='/mnt/c/Users/LEEHQ/Downloads/chinamap/NE1_LR_LC_SR_W_DR.tif'
    ax.imshow(imread(fname), origin='upper', transform=proj, extent=[-180, 180, -90, 90])
    # --设置网格点属性
    gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=1.2, color='k', alpha=0.5, linestyle='--')
    gl.xlabels_top = False  # 关闭顶端的经纬度标签
    gl.ylabels_right = False  # 关闭右侧的经纬度标签
    gl.xformatter = LONGITUDE_FORMATTER  # x轴设为经度的格式
    gl.yformatter = LATITUDE_FORMATTER  # y轴设为纬度的格式
    gl.xlocator = mticker.FixedLocator(np.arange(extent[0], extent[1]+10, 10))
    gl.ylocator = mticker.FixedLocator(np.arange(extent[2], extent[3]+10, 10))
    return ax

if __name__ == '__main__':
    warnings.filterwarnings('ignore')
    ax = create_map()
    plt.show()

图形如下:

Figure_1.png

相关文章

网友评论

      本文标题:Python气象绘图(三):中国地图

      本文链接:https://www.haomeiwen.com/subject/mylaactx.html