- 积分
- 46
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2020-6-2
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.path as mpath
import cartopy.crs as ccrs
import cartopy.mpl.ticker as ctk
import frykit.plot as fplt
# In[]
#等值线数据准备
nc_path1='D:/ncfile/20230716gk.nc'
file1 = xr.open_dataset(nc_path1)
print(file1)
file_500=file1.sel(valid_time=slice('2023-07-16T01:00','2023-07-20T00:00'))
file_500_mean=file_500.mean(['valid_time'])
print(file_500_mean)
contours_ds=file_500_mean['z'].sel(pressure_level=500).values/98
lons=file1['longitude']
lats=file1['latitude']
contours_levels=np.arange(500,596,4)
u=file_500_mean['u'].sel(pressure_level=500).values
v=file_500_mean['v'].sel(pressure_level=500).values
# In[]
#画图准备
#1、定义投影
#地图投影
map_crs=ccrs.LambertConformal(central_longitude=105,central_latitude=35,standard_parallels=(20.0,40.0))
#数据投影
data_crs=ccrs.PlateCarree()
#2、中文字符显示
plt.rcParams['font.sans-serif']=['Kaiti'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
fig=plt.figure(figsize=(9,8),dpi=400,linewidth=1)
#定义了地图的经度和纬度范围,即左侧经度、右侧经度、下方纬度和上方纬度。
lon1,lon2,lat1,lat2=[70,140,15,55]
# -----------------------------------------------------------------------------
# 创建了一个矩形路径,用于表示地图的边界范围,并使用150个插值点对其进行插值处理。
rect=mpath.Path([[lon1,lat1],[lon2,lat1],
[lon2,lat2],[lon1,lat2],[lon1,lat1]]).interpolated(150)
# 定义了一个兰伯特等角投影,设置了中心经度和中心纬度为地图范围的中心点。
proj=ccrs.LambertConformal(central_longitude=(lon1+lon2)*0.5,
central_latitude=(lat1+lat2)*0.5)
# 在图形上创建一个子图,指定了投影类型为之前定义的兰伯特等角投影。
ax=fig.subplots(1,1,subplot_kw={'projection':proj})
# ---关键部分-------关键部分------------------关键部分-------------------关键部分----------
proj_to_data=ccrs.PlateCarree()._as_mpl_transform(ax)-ax.transData
rect_in_target=proj_to_data.transform_path(rect)
ax.set_boundary(rect_in_target)
ax.set_xlim(rect_in_target.vertices[:,0].min(),rect_in_target.vertices[:,0].max())
ax.set_ylim(rect_in_target.vertices[:,1].min(),rect_in_target.vertices[:,1].max())
# 在地图上添加海洋特征,设置线宽和颜色。
fplt.add_land(ax, lw=0.3) #添加陆地
fplt.add_countries(ax,lw=0.3) #添加国界
fplt.add_cn_province(ax, lw=1) #添加中国省份边界,和南海九段线
# fplt.label_cn_province(ax,size=6,color='red',font='Kaiti')
# fplt.add_cn_city(ax,province='湖南省',lw=0.1)
# fplt.label_cn_city(ax,province='湖南省',size=2,color='red',font='Kaiti')
fplt.add_compass(ax, 0.95,0.8,size=15) #添加指北针
fplt.add_map_scale(ax, 0.1, 0.1,500) #添加比例尺
ax.spines['bottom'].set_linewidth('0.2')
gl=ax.gridlines(draw_labels=True,x_inline=False,y_inline=False,linestyle='dashed')
gl.top_labels=False
gl.right_labels=False
gl.rotate_labels=False
gl.xlocator=ctk.LongitudeLocator(4)
gl.ylocator=ctk.LatitudeLocator(6)
gl.xformatter=ctk.LongitudeFormatter(zero_direction_label=True)
gl.yformatter=ctk.LatitudeFormatter()
#---------------------画等值图------------------------------------------
w_lines = ax.contour(lons, lats,contours_ds, #绘制等值线图
levels=contours_levels, #设置w的范围
linewidths=1.5, #设置线宽
colors='tab:brown', #设置等值线颜色
linestyles='-', #设置等值线样式
transform=data_crs)
w_t_labels = ax.clabel(w_lines, #设置w的标签
inline=True, #'True'表示在放置标签的位置移除基础轮廓
fmt='%.0f', #设置标签格式为浮点数
fontsize=10, #设置标签字体尺寸
colors='black' #设置标签字体颜色
)
#画风羽图
wind_slice = (slice(None, None, 5), slice(None, None, 5))
ax.barbs(lons[wind_slice[0]], lats[wind_slice[1]],
u[wind_slice], v[wind_slice],
color='blue',
linewidth=0.8, #宽度
length=4, #长度
sizes=dict(emptybarb=0),
barb_increments=dict(half=2, full=4, flag=20),
transform=data_crs)
|
-
|