NASA官网下载的高分辨率底图(https://www.naturalearthdata.com/downloads/10m-raster-data/10m-cross-blend-hypso/)
非常漂亮,效果如下:
那么画图的时候,想用这个图来作为底图。需要用到imshow函数。
首先加载库
import matplotlib.pyplot as plt
import matplotlib.colors as colors
from cartopy.io.shapereader import Reader
from shapely.geometry import Polygon,Point
from shapely.geometry.multipolygon import MultiPolygon
from shapely.ops import linemerge,polygonize_full
from shapely.ops import cascaded_union
from cartopy.mpl.patch import geos_to_path
import matplotlib.patches as mpatches
import pandas as pd
import numpy as np
import os,datetime
import sys
import cmaps
import xarray as xr
import netCDF4 as nc
from scipy.io import loadmat
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import sys, os
import importlib
import matplotlib
from pylab import mpl
font = {'family' : 'Arial',
'weight' : 'medium',
'size' : 20,
'style' : 'normal'}
font0 = {'family' : 'Arial',
'weight' : 'medium',
'size' : 20,
'style' : 'italic'}
mpl.rcParams['mathtext.fontset'] = 'custom'
mpl.rcParams['mathtext.it'] = 'Arial:italic'
mpl.rcParams['mathtext.rm'] = 'Arial'
matplotlib.rc('font', **font)
matplotlib.rc('text', usetex=False)
以全球历史台风路径为例。首先读取数据
dt=xr.open_dataset(r'C:\Users\wangnan\Desktop\Allstorms.ibtracs_wmo.v03r08.nc')
lat=dt.lat_wmo.values
lon=dt.lon_wmo.values
speed=dt.wind_wmo.values
ns,nt=np.shape(speed)
设置填色
print('{highlight:#^50s}'.format(highlight='load RGB '))
#rgb=([27,41,224],[47,81,224],[81,120,234],[130,173,239],[231,186,100],[255,124,0],[255,69,0])
rgb=([61,135,255],[117,211,255],[210,245,255],[255,217,217],[255,115,115],[255,64,64],[255,13,13],)
rgb=np.array(rgb)/255.0
icmap=colors.ListedColormap(rgb,name='my_color')
cmap_color=icmap #.reversed()
画图
xstep = 60 #x axis step
ystep = 60 #y axis step
fig=plt.figure(num="figure1",figsize=(22,8)) ###ͼ??½?fig
clevs=np.array([0,17,33,43,50,59,70,80]) #填色范围,依据中心速度划分台风等级
proj=ccrs.PlateCarree()
fig,ax1 = plt.subplots(1, 1,dpi=300, subplot_kw={'projection': proj})
# ax1.stock_img()#自带的低分辨率的底图
# NASA官网下载的高分辨率底图(https://www.naturalearthdata.com/downloads/10m-raster-data/10m-cross-blend-hypso/)
ax1.imshow(plt.imread(r'C:\Users\wangnan\Desktop\HYP_LR_SR_W_DR\HYP_LR_SR_W_DR.tif')
, origin='upper', transform=ccrs.PlateCarree(), extent=[-180, 180, -90, 90])
ax1.coastlines()
ax1.set_xticks(np.arange(-180, 181, 60), crs = proj)
ax1.set_yticks(np.arange(-90, 91,30), crs = proj)
ax1.xaxis.set_major_formatter(LongitudeFormatter())
ax1.yaxis.set_major_formatter(LatitudeFormatter())
#ax1.set_xlim(60,120) #这里控制底图范围
#ax1.set_ylim(0,60)
ax1.minorticks_on()
#————————————————
for i in range(nt):
normc = colors.BoundaryNorm(clevs, ncolors=cmap_color.N, clip=True)
c3=plt.scatter(lon[:,i],lat[:,i],s=1,c=speed[:,i],norm=normc,cmap=cmap_color,alpha=0.8)
plt.tick_params(labelsize=12)
position = fig.add_axes([0.235, 0.11, 0.58, 0.030])
clevs= np.array ([8.5, 25, 38, 46.5, 54.5, 64.5, 75])
cbar = plt.colorbar( c3,cax=position,ticks=clevs,orientation='horizontal',)
cbar.ax.set_xticklabels(['TD\n <17', 'TS\n 17-33', 'C1\n 33-43',\
'C2\n 43-50', 'C3\n 50-59', 'C4\n 59-70', 'C5\n >70'],size=12)
cbar.set_label('wind speed (m/s)',size=12,weight='normal')
plt.savefig('./typhoon_track.png', dpi=300, bbox_inches='tight')
plt.show()