立即注册 登录
气象家园 返回首页

haloyang的个人空间 http://bbs.06climate.com/?84538 [收藏] [复制] [分享] [RSS]

日志

PYTHON学习2-中国地图

已有 15 次阅读2021-11-2 22:55 |个人分类:python| 中国地图

import maskout
import maskout2
import os
import xarray as xr
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeat
from cartopy.io.shapereader import Reader
import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import cartopy.io.shapereader as shpreader
from cartopy.mpl.ticker import LongitudeFormatter,LatitudeFormatter
from matplotlib import rcParams
from matplotlib.font_manager import FontProperties
font=FontProperties(fname='/mnt/c/Windows/Fonts/arial.ttf',size=28)


SHP = '/mnt/f/shp_files/china_basic_map/'
# 数据读取
ds = xr.open_dataset('/mnt/c/Users/haloyoung/Desktop/CN05.1_Pre_daily_19610101-19611231.nc')
pr = ds['preci']

#取一段时间内的降水作为绘图的数据源
pr_ave=pr.sum(dim='time')
lon=ds.lon
lat=ds.lat
lon_range=lon[(lon>70)&(lon<140)]
lat_range=lat[(lat>0)&(lat<60)]
pr_region=pr_ave.sel(lon=lon_range,lat=lat_range)  #选择某区域

# 创建画图空间
proj = ccrs.PlateCarree()  #创建投影
fig  = plt.figure(figsize=(16,12))  #创建页面
ax   = fig.subplots(1, 1, subplot_kw={'projection': proj})  #主图

# 设置网格点属性
region = [70, 137, 17, 55]
ax.set_extent(region,crs=proj)
ax.set_xticks(np.arange(region[0],region[1]+1,15),crs=proj)
ax.set_xticks(np.arange(region[0],region[1]+1,5),minor=True,crs=ccrs.PlateCarree())
ax.set_yticks(np.arange(region[-2]+3,region[-1]+4,10),crs=proj)
ax.set_yticks(np.arange(region[-2]+3,region[-1]+4,5),minor=True,crs=ccrs.PlateCarree())
ax.set_yticklabels(['20','30','40','50'])

ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=False))
ax.yaxis.set_major_formatter(LatitudeFormatter())
for l in ax.xaxis.get_ticklabels():
    l.set_font_properties(font)
for l in ax.yaxis.get_ticklabels():
    l.set_font_properties(font)

#网格线设置
#ax.grid(linestyle = '--')  
# ax.grid(linewidth=1.2,color='r',alpha=0.2,linestyle='--')

# 画图
levels = np.arange(0,2500,100) #第三个参数标识间隔
ax.set_title('annual total precipitation (mm)',loc='left',fontproperties=font)
cs     = ax.contourf(lon_range,lat_range,pr_region,levels=levels,cmap='Spectral_r',transform=ccrs.PlateCarree(),extend='both')
position=fig.add_axes([0.92, 0.2, 0.02, 0.59])
b      = plt.colorbar(cs,cax=position,shrink=0.88,orientation='vertical',extendrect=True,pad=0.035,aspect=20)
for l in b.ax.yaxis.get_ticklabels():
    l.set_font_properties(font)

#中国白化
ax.add_geometries(Reader(os.path.join(SHP,'bou2_4l.shp')).geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='k',linewidth=1)
clip   = maskout.shp2clip(cs,ax,'/mnt/c/Users/haloyoung/Desktop/Python/country1.shp',"China")


#添加南海,实际上就是新建一个子图覆盖在之前子图的右下角
f2_ax2 = fig.add_axes([0.8,0.2,0.1,0.17],projection=proj)   #fig.add_axes([left, bottom, width, height]),南海子图摆放的位置和大小
f2_ax2.set_extent([105,125,0,25],crs=ccrs.PlateCarree())   #地图范围
# f2_ax2.add_feature(cfeature.COASTLINE.with_scale('50m')) ##不画,让南海图简单一点
# f2_ax2.add_feature(cfeature.LAND.with_scale('50m'))  ##不画,让南海图简单一点
china  = shpreader.Reader('/mnt/f/shp_files/china_basic_map/bou2_4l.dbf').geometries()
f2_ax2.add_geometries(china,ccrs.PlateCarree(),facecolor='none',edgecolor='black',zorder=1)
cs3    = f2_ax2.contourf(lon_range,lat_range,pr_region,range(0,3000,500),cmap='Spectral_r')

#南海的白化
f2_ax2.add_geometries(Reader(os.path.join(SHP,'bou2_4l.shp')).geometries(),ccrs.PlateCarree(),facecolor='none',edgecolor='k',linewidth=1)
clip   = maskout2.shp2clip(cs3,f2_ax2,'/mnt/f/shp_files/china_basic_map/China_10-dash_line.shp') #中国九段线

f  = plt.gcf()
plt.show()
f.savefig('/mnt/c/Users/haloyoung/Desktop/plot.png',format='png')  ###plt.show()放在f.savefig前,能够保证save的图片和show的尺寸
f.clear() #释放内存

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

返回顶部