使用MetPy声明式语法在卫星影像上绘制经度线区分MJO相位咨询
MJO相位分隔经度线绘制需求
我想要实现MJO不同区域/相位的可视化,计划通过绘制分隔各相位区域的经度线(大致为60E、80E、100E、120E、140E、160E、180)完成该需求,不确定能不能在现有绘图代码基础上添加该功能。我使用的是NCEI提供的GRID-Sat B1数据,现有代码如下:
import matplotlib.pyplot as plt from metpy.plots import declarative, colortables import cartopy.crs as ccrs import xarray as xr file = "GRIDSAT-B1.2003.11.23.00.v02r01.nc" dataset = xr.open_dataset(file) vtime = dataset.time.values.astype('datetime64[s]').astype('O') date_long = vtime[0] date = date_long.strftime("%d-%b-%Y-%HZ") # 水汽图像绘制 img = declarative.ImagePlot() img.data = dataset img.field = 'irwvp' img.colormap = 'WVCIMSS_r' img.image_range = (180, 280) panel = declarative.MapPanel() panel.layers = ['coastline', 'borders'] panel.title = f'GridSat-B1 (Water Vapor Imagery): {date}' panel.projection = (ccrs.Mollweide(central_longitude=-240)) panel.area = ([-370, -140, -30, 30]) panel.layout = (2, 1, 2) panel.plots = [img] # 红外图像绘制 img2 = declarative.ImagePlot() img2.data = dataset img2.field = 'irwin_cdr' img2.colormap = 'turbo_r' # 也可尝试用cubehelix配色 img2.image_range = (180, 300) panel2 = declarative.MapPanel() panel2.layers = ['coastline', 'borders'] panel2.title = f'GridSat-B1 (Infrared Imagery): {date}' panel2.projection = (ccrs.Mollweide(central_longitude=-240)) panel2.area = ([-370, -140, -30, 30]) panel2.layout = (2, 1, 1) panel2.plots = [img2] # 多面板容器配置 pc = declarative.PanelContainer() pc.size = (20, 14) pc.panels = [panel, panel2] pc.show()
运行脚本后当前输出为水汽、红外云图上下排列的布局结果。
实现方案
直接在现有代码基础上修改即可,不需要重构原有逻辑。MetPy的声明式绘图接口完成初始化后,可以直接获取每个面板的轴对象,手动添加你需要的分隔经线即可。
你只需要在pc.show()之前添加如下代码段:
# 定义MJO相位分隔经度列表 mjo_lons = [60, 80, 100, 120, 140, 160, 180] # 先触发绘图初始化,生成轴对象 pc.draw() # 给两个面板都添加分隔线 for panel in pc.panels: for lon in mjo_lons: # 绘制垂直经线,参数可根据显示效果调整 panel.ax.plot( [lon, lon], [-30, 30], color='white', linewidth=2, alpha=0.8, transform=ccrs.Geodetic() )
参数调整说明
- 觉得白色分隔线在云图上辨识度不够的话,可以把
color改成'red'、'yellow'等其他颜色 - 线宽可以通过调整
linewidth的数值修改,alpha控制线条透明度 - 如果需要标注经度值,可以在
plot之后再加panel.ax.text(lon, 32, f'{lon}°E', transform=ccrs.Geodetic(), ha='center', color='white')实现经线顶部标注
修改后的完整代码如下:
import matplotlib.pyplot as plt from metpy.plots import declarative, colortables import cartopy.crs as ccrs import xarray as xr file = "GRIDSAT-B1.2003.11.23.00.v02r01.nc" dataset = xr.open_dataset(file) vtime = dataset.time.values.astype('datetime64[s]').astype('O') date_long = vtime[0] date = date_long.strftime("%d-%b-%Y-%HZ") # 水汽图像绘制 img = declarative.ImagePlot() img.data = dataset img.field = 'irwvp' img.colormap = 'WVCIMSS_r' img.image_range = (180, 280) panel = declarative.MapPanel() panel.layers = ['coastline', 'borders'] panel.title = f'GridSat-B1 (水汽图像): {date}' panel.projection = ccrs.Mollweide(central_longitude=-240) panel.area = ([-370, -140, -30, 30]) panel.layout = (2, 1, 2) panel.plots = [img] # 红外图像绘制 img2 = declarative.ImagePlot() img2.data = dataset img2.field = 'irwin_cdr' img2.colormap = 'turbo_r' img2.image_range = (180, 300) panel2 = declarative.MapPanel() panel2.layers = ['coastline', 'borders'] panel2.title = f'GridSat-B1 (红外图像): {date}' panel2.projection = ccrs.Mollweide(central_longitude=-240) panel2.area = ([-370, -140, -30, 30]) panel2.layout = (2, 1, 1) panel2.plots = [img2] # 容器配置 pc = declarative.PanelContainer() pc.size = (20, 14) pc.panels = [panel, panel2] # 新增:添加MJO相位分隔线 mjo_lons = [60, 80, 100, 120, 140, 160, 180] pc.draw() for p in pc.panels: for lon in mjo_lons: p.ax.plot([lon, lon], [-30, 30], color='white', linewidth=2, alpha=0.8, transform=ccrs.Geodetic()) # 可选:添加经度标注 # p.ax.text(lon, 32, f'{lon}°E', ha='center', color='white', transform=ccrs.Geodetic()) pc.show()
内容的提问来源于stack exchange,提问作者bedettmann
相关产品推荐
相关产品推荐

