You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

contourf绘制850hPa温度波1缺失负向区域,层级步长影响问题

问题:contourf层级步长设置导致850hPa气温波1负向区域无法显示

我尝试用contourf绘制850hPa气温的波1-4分量,波2-4绘制正常,但波1仅显示正值区域,负向区域完全缺失。所有波数均通过相同FFT方法处理,数据本身无错误。

当将层级步长从0.5调整为0.1时,波1的负向区域就能正常显示。想了解为何contourf的层级设置会引发这种颜色区域缺失的问题。


代码片段

from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.util import add_cyclic_point
import cmaps

t_all = Dataset('t850_all_wave.nc')
t_cli = Dataset('t850_cli_wave.nc')
print(t_all.variables.items())

lat = t_all.variables["latitude"][:]
lon = t_all.variables["longitude"][:]

t_all = t_all.variables["msl"][:]
t_cli = t_cli.variables["msl"][:]

t = np.concatenate((np.array(t_all), np.array(t_cli)))

fig = plt.figure(figsize=(7,5))
ax_list = []

title = ['(a) t850 - wave1','(b) t850 - wave2','(c) t850 - wave3','(d) t850 - wave4']

# 定义极地图边界
circle = plt.Circle((0.5, 0.5), 0.5, transform=fig.transFigure)

for i in range(2):
    for j in range(2):
        
        ax = fig.add_axes([0.+0.42*j, 0.+0.6*(1-i), 0.5, 0.5],projection = ccrs.NorthPolarStereo(central_longitude=0))

        ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False,linewidth=1, color='grey',linestyle='--')

        ax.set_boundary(circle, transform=ax.transAxes)

        ax.set_extent((-180,180,0,90),ccrs.PlateCarree())
        
        cycle_data, cycle_lon = add_cyclic_point(t[2*i+j], coord=lon)
        
        lv = np.arange(-5,5.1,0.5)

        contour1 = ax.contourf(cycle_lon,lat,cycle_data,levels=lv,transform=ccrs.PlateCarree(),cmap=cmaps.BlueWhiteOrangeRed,extend='both')
        
        contour2 = ax.contour(lon,lat,t[2*i+j+4],levels=[-5,-4.5,-4,-3.5,-3,-2.5,-2,-1.5,-1,-0.5,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5],colors="black",transform=ccrs.PlateCarree(),linewidths=1.1)
        
        ax.coastlines(lw = 0.5)
        
        plt.title(title[2*i+j],loc="left")
        
        ax_list.append(ax)

position=fig.add_axes([0.92, 0.05, 0.02, 0.95])
cb = plt.colorbar(contour1,ax=ax_list,cax=position,aspect=30,orientation='vertical',label='Wave 1-4 of t850')

plt.savefig("t850.png", dpi=500, bbox_inches='tight')

原因分析与解决

核心原因

问题出在波1负向区域的数值范围与你设置的层级区间不匹配:

  • 当使用lv = np.arange(-5,5.1,0.5)时,层级序列的负向刻度是-5, -4.5, -4,..., -0.5, 0。如果波1的负向极值仅在(-0.5, 0)区间内(比如最小值为-0.4),这些数值会被归到“低于最低负向层级”的延伸区,但由于步长较大(0.5),加上北极投影的坐标转换特性,contourf无法正确渲染这部分接近0的弱信号区域。
  • 当步长改为0.1后,层级序列会包含-0.4, -0.3等更精细的刻度,波1的负向数值能匹配到对应的层级区间,因此可以正常显示。

验证与解决方法

  1. 验证数据范围:
    打印波1数据的极值:

    print(f"波1最小值: {np.min(t[0])}, 最大值: {np.max(t[0])}")
    

    确认负向数值是否落在(-0.5, 0)区间内。

  2. 针对性解决:

    • 动态生成层级:根据波1的数据范围自动生成适配的层级,确保覆盖所有数值:
      wave1_min = np.min(t[0])
      wave1_max = np.max(t[0])
      lv = np.linspace(wave1_min - 0.1, wave1_max + 0.1, 20)
      
    • 手动调整层级:如果波1负向极值接近0,可在层级中添加更贴近的刻度,比如:
      lv = np.concatenate([np.arange(-5, -0.5, -0.5), np.arange(-0.5, 0.1, 0.1), np.arange(0.5, 5.1, 0.5)])
      
    • 检查延伸区渲染:确保extend='both'生效,可尝试将最低层级设为比波1最小值更低的数值,比如lv = np.arange(-0.6,5.1,0.5),让负向数值落入正常层级区间而非延伸区。

内容的提问来源于stack exchange,提问作者potatopotato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 07:26:19