# %%
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
from matplotlib.font_manager import FontProperties
mpl.rcParams["font.size"] = 13
font_cn = FontProperties(fname="C:/Users/xxx/anaconda3/Lib/site-packages/matplotlib/mpl-data/fonts/chinese_ttf/方正楷体简体.ttf", size=14)
# read data
data_path = "D:/python/CSDN/data/ERA5-pressure_20230301-0304.nc"
time_range = pd.date_range('2023-02-01', periods=24*3+1, freq='H')
ds = xr.open_dataset(data_path).sel(time=time_range)
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 = 39.8, 116.47
level = [100,150,200,300,400,500,600,700,800,850,900,925,1000]
rh_bj= rh.sel(latitude = lat, longitude = lon, level = level, method = 'nearest').transpose("level","time")[::-1, ::-3].values
temp_bj= temp.sel(latitude = lat, longitude = lon, level = level, method = 'nearest').transpose("level","time")[::-1, ::-3].values
u_bj= u.sel(latitude = lat, longitude = lon, level = level, method = 'nearest').transpose("level","time")[::-1, ::-3].values
v_bj= v.sel(latitude = lat, longitude = lon, level = level, method = 'nearest').transpose("level","time")[::-1, ::-3].values
w_bj= w.sel(latitude = lat, longitude = lon, level = level, method = 'nearest').transpose("level","time")[::-1, ::-3].values
time = rh['time'][::-3]
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=1)
temp_bj = gaussian_filter1d(temp_bj, sigma=0.5)
以上是利用ERA5的原始数据绘制综合廓线图的读取数据部分,如果自己有其他模式或者其他格式的数据,只要处理成(level, time)的二维数据,其中level和time坐标都是从大到小排列就好。
#建立画布
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,3,5,7,10,12]
y_labels = [1000,925,850,700,500,200,100]
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轴次刻度显示间隔
#添加标题、xlabel等图片说明信息
ax.text(23,-2,'Feb',color='r')
ax.text(0,-2,'北京时',fontproperties = font_cn)
ax.text(0,13.3,'Lat:'+str(lat)+' Lon:'+str(lon),size=15,color='r')
ax.text(21,13.3,'000-072',size=15,color='b')
ax.text(10,13.3,'2023020100',size=15,color='b')
ax.text(7,14,'单点3小时间隔综合廓线图', fontproperties = font_cn, size=22,color='b')
ax.set_ylabel("hPa")
大功告成!
# %%
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
from matplotlib.font_manager import FontProperties
mpl.rcParams["font.size"] = 13
font_cn = FontProperties(fname="C:/Users/xxx/anaconda3/Lib/site-packages/matplotlib/mpl-data/fonts/chinese_ttf/方正楷体简体.ttf", size=14)
# %%
# read data
data_path = "D:/python/CSDN/data/ERA5-pressure_20230301-0304.nc"
time_range = pd.date_range('2023-02-01', periods=24*3+1, freq='H')
ds = xr.open_dataset(data_path).sel(time=time_range)
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 = 39.8, 116.47
level = [100,150,200,300,400,500,600,700,800,850,900,925,1000]
rh_bj= rh.sel(latitude = lat, longitude = lon, level = level, method = 'nearest').transpose("level","time")[::-1, ::-3].values
temp_bj= temp.sel(latitude = lat, longitude = lon, level = level, method = 'nearest').transpose("level","time")[::-1, ::-3].values
u_bj= u.sel(latitude = lat, longitude = lon, level = level, method = 'nearest').transpose("level","time")[::-1, ::-3].values
v_bj= v.sel(latitude = lat, longitude = lon, level = level, method = 'nearest').transpose("level","time")[::-1, ::-3].values
w_bj= w.sel(latitude = lat, longitude = lon, level = level, method = 'nearest').transpose("level","time")[::-1, ::-3].values
time = rh['time'][::-3]
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=1)
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,3,5,7,10,12]
y_labels = [1000,925,850,700,500,200,100]
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轴次刻度显示间隔
#添加标题、xlabel等图片说明信息
ax.text(23,-2,'Feb',color='r')
ax.text(0,-2,'北京时',fontproperties = font_cn)
ax.text(0,13.3,'Lat:'+str(lat)+' Lon:'+str(lon),size=15,color='r')
ax.text(21,13.3,'000-072',size=15,color='b')
ax.text(10,13.3,'2023020100',size=15,color='b')
ax.text(7,14,'单点3小时间隔综合廓线图', fontproperties = font_cn, size=22,color='b')
ax.set_ylabel("hPa")
plt.savefig("综合廓线图.jpg")