- 积分
- 172
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2021-10-8
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.pyplot import MultipleLocator #导入此类,设置坐标轴间隔
import pandas as pd
from scipy.ndimage import gaussian_filter1d
mpl.rcParams["font.size"] = 13
#读取并挑选数据
data_path = "D:/pythonProjec/19.nc"
start_time='2024-05-19T08:00:00'
end_time='2024-05-19T12:00:00'
ds = xr.open_dataset(data_path).sel(time=slice(start_time,end_time))
rh, temp, u, v, w = ds['r'], ds['t']-273.15, ds['u'], ds['v'], ds['w'] # (time,level, lat, lon)
# 挑选站点,转换time, level坐标顺序,并调整level(1000hPa->100hPa),time排序
lat, lon = 32.5, 108.3
level = [200, 250, 300, 350, 400, 450, 500, 550,600,650,700,750, 800, 850, 900]
rh_bj = rh.sel(latitude=lat, longitude=lon, level=level, method='nearest').transpose("level", "time")[::-1, ::1].values
temp_bj = temp.sel(latitude=lat, longitude=lon, level=level, method='nearest').transpose("level", "time")[::-1,
::1].values
u_bj = u.sel(latitude=lat, longitude=lon, level=level, method='nearest').transpose("level", "time")[::-1, ::1].values
v_bj = v.sel(latitude=lat, longitude=lon, level=level, method='nearest').transpose("level", "time")[::-1, ::1].values
w_bj = w.sel(latitude=lat, longitude=lon, level=level, method='nearest').transpose("level", "time")[::-1, ::1].values
time = rh['time'][::1]
#世界时转为需要时区
time_str = pd.to_datetime(time, utc=True).tz_convert('Asia/Shanghai').strftime(
'%d%H').tolist() # datetime64转为string,和utc+8
# 平滑等高线数据
rh_bj = gaussian_filter1d(rh_bj, sigma=0.5) # sigma数值越大 越平滑
w_bj = gaussian_filter1d(w_bj, sigma=0.5)
temp_bj = gaussian_filter1d(temp_bj, sigma=0.5)
# 建立画布
fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(1, 1, 1)
# 添加RH填色图
rh_plot = ax.contourf(rh_bj, cmap='Greens', levels=[60, 70, 80, 90, 95], extend='both', alpha=0.7)
fig.colorbar(rh_plot, ax=ax, shrink=0.6, pad=0.02, extendfrac='auto') # 添加label bar
# 添加temp, w等高线
temp_plot = ax.contour(temp_bj, colors='r') # Negative contours default to dashed.
w_plot = ax.contour(w_bj, colors='k', alpha=0.7)
ax.clabel(temp_plot, inline=True, fontsize=10) # 添加等高线label
ax.clabel(w_plot, inline=True, fontsize=10)
# 画风向标
barb_increments = dict(half=2, full=4, flag=20)
ax.barbs(u_bj, v_bj, color='b', length=7, alpha=0.8, barb_increments=barb_increments)
# 添加横纵坐标对应的时间和高度
# y轴
y_pos = [0, 1, 4, 6, 8, 14]
y_labels = [900, 850, 700, 600, 500, 200]
ax.set_yticks(y_pos, labels=y_labels) # 显示特定xticklabels
ax.set_ylim(-0.5, len(level) + 0.1) # 上下留出一点空白
# x轴
x_pos = np.arange(len(time_str))
ax.set_xticks(x_pos, labels=time_str)
ax.xaxis.set_major_locator(MultipleLocator(4)) # 设置x轴主刻度显示间隔
ax.xaxis.set_minor_locator(MultipleLocator(2)) # 设置x轴次刻度显示间隔
ax.set_ylabel("hPa")
plt.savefig("D:/pythonProjec/综合廓线图.jpg")
|
|