xarray 绘制任意两点剖面图

metpy中的cross_section提供了非常便捷的绘制剖面图的方法,具体可见网:
https://unidata.github.io/MetPy/latest/examples/cross_section.html#sphx-glr-examples-cross-section-py

如果只是简单的画个抛面                                     

复制代码
# %%
import xarray as xr
import proplot as pplt
from metpy.interpolate import cross_section
# %%
era5fnm = r'F:/era5/era5_2021071900_2021072023/era5_2021071900UTC.nc'
era5f = xr.open_dataset(era5fnm)
era5f = era5f.metpy.parse_cf().squeeze()
# %%
# create cross section
start = (12.0, 96.0)
end = (27.0, 120.0)
cross = cross_section(era5f, start, end)
# %%
fig = pplt.figure(
    refwidth = 5.0,
    )
ax = fig.subplot(
    )
# plot cross section
ax.contourf(
    cross['g0_lat_2'],
    cross['lv_ISBL1'][::-1],
    cross['V_GDS0_ISBL'][::-1, :],
    )
ax.quiver(
    cross['g0_lat_2'][::4],
    cross['lv_ISBL1'][::-1],
    cross['V_GDS0_ISBL'][::-1, ::4],
    cross['W_GDS0_ISBL'][::-1, ::4]*10.,
    )
复制代码

如果你想要为剖面添加地形并且显示剖面的平面位置和一些场变量 

 

复制代码
# %%
import xarray as xr
import proplot as pplt
from metpy.interpolate import cross_section
import cartopy.crs as ccrs
import cartopy.feature as cfeat
from cartopy.io.shapereader import Reader
# %%
era5fnm = r'F:/era5/era5_2021071900_2021072023/era5_2021071900UTC.nc'
era5f = xr.open_dataset(era5fnm)
era5f = era5f.metpy.parse_cf().squeeze()
terfnm = r'F:/terrain/dixingdata.nc'
terf = xr.open_dataset(terfnm)
terf = terf.metpy.parse_cf().squeeze()
c = terf['Y'].loc[30:10]
# %%
# create cross section
start = (12.0, 96.0)
end = (27.0, 120.0)
cross = cross_section(era5f, start, end)
tercross = cross_section(terf, start, end)
# %%
fig = pplt.figure(
    refwidth = 5.0,
    )
ax = fig.subplot(
    )
# plot cross section
ax.contourf(
    cross['g0_lat_2'],
    cross['lv_ISBL1'][::-1],
    cross['V_GDS0_ISBL'][::-1, :],
    )
ax.quiver(
    cross['g0_lat_2'][::4],
    cross['lv_ISBL1'][::-1],
    cross['V_GDS0_ISBL'][::-1, ::4],
    cross['W_GDS0_ISBL'][::-1, ::4]*10.,
    )
# plot terrain
oy = ax.alty()
oy.area(
    cross['g0_lat_2'],
    tercross['elev'],
    c = 'grey',
    )
oy.format(
    ylim = (0, 10000),
    yticks = 'none',
    ylabel = '',
    )
# plot inset panel
data_crs = era5f['V_GDS0_ISBL'].metpy.cartopy_crs
ix = fig.add_axes(              # do note that using ax.inset() of proplot will cause a geo stale error
    [0.105, 0.685, 0.4, 0.3],   # thus using fig.add_axes alternatively
    projection = data_crs,
    )
ix.format(
    lonlim = (95, 125),
    latlim = (10, 30),
    )
ix.contour(
    era5f['g0_lon_3'].loc[95:125],
    era5f['g0_lat_2'].loc[10:30],
    era5f['Z_GDS0_ISBL'].loc[850, 10:30, 95:125],
    c = 'blue',
    )
ix.line(
    cross['g0_lon_3'],
    cross['g0_lat_2'],
    c = 'red',
    )
# add geographic information
ix.coastlines()
provinces = cfeat.ShapelyFeature(
    Reader(r'F:/ngcc/bou2_4m/bou2_4l.shp').geometries(),
    ccrs.PlateCarree(), 
    edgecolor='black',
    facecolor='none',
    )
river = cfeat.ShapelyFeature(
    Reader(r'F:/Chinamap-master/cnmap/rivers.shp').geometries(),
    ccrs.PlateCarree(), 
    edgecolor='lightblue',
    facecolor='none',
    )
ix.add_feature(provinces, linewidth=0.5, zorder=2)
ix.add_feature(river, linewidth=1.0, zorder=2)
复制代码

 

 

 

参考 https://mp.weixin.qq.com/s?__biz=MzA4MTAzMjQzMQ==&mid=2651703908&idx=1&sn=dcd98d180acdc604bf258a49eae80fd8&chksm=84624fe4b315c6f204db162278870c2e5ad35c22e681310c837239326eeefc8f1240d668c3a7#rd   出自气象家园

 

posted on   闹不机米  阅读(2427)  评论(1编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示