pyart中'pyart_HomeyerRainbow'色值无效报错求助
解决
pyart_HomeyerRainbow色标找不到的报错问题 核心原因
本地环境中PyART的自定义色标未正确注册到Matplotlib,或版本不匹配导致色标名称/注册逻辑存在差异。
具体解决步骤
确认PyART版本一致性
运行以下代码查看当前PyART版本,和其他使用者的版本对比:import pyart print(pyart.__version__)如果版本不一致,安装和其他人相同的版本:
pip install pyart==<对应版本号>手动注册PyART色标
在代码中import pyart之后,添加手动注册色标的代码,确保Matplotlib能识别PyART的自定义色标:import pyart # 手动注册所有PyART色标 pyart.graph.cm.generate_cmap()之后再调用
get_cmap('pyart_HomeyerRainbow')即可正常识别。直接调用PyART内置色标(推荐新手)
绕开Matplotlib的get_cmap,直接使用PyART自带的色标对象,避免注册问题:
将原代码中的:cf = plt.contourf(x, y, dbz, levels=clevs, cmap=get_cmap('pyart_HomeyerRainbow'))修改为:
cf = plt.contourf(x, y, dbz, levels=clevs, cmap=pyart.graph.cm.HomeyerRainbow)部分旧版本中色标名称不带
pyart_前缀,直接用HomeyerRainbow即可。检查虚拟环境(若使用)
如果用了虚拟环境,确认代码运行时激活的是正确的环境。运行以下命令查看当前Python路径:# Linux/macOS which python # Windows where python若路径不对,激活对应环境后重新安装PyART。
修改后的完整代码示例
from netCDF4 import Dataset import matplotlib.pyplot as plt import cartopy.crs as crs from cartopy.feature import NaturalEarthFeature import numpy as np import pyart from wrf import (to_np, getvar, smooth2d, get_cartopy, cartopy_xlim, cartopy_ylim, latlon_coords) import warnings warnings.filterwarnings("ignore", category=DeprecationWarning) # 可选:手动注册PyART色标(若继续用get_cmap则需要) # pyart.graph.cm.generate_cmap() times = ['000070'] level = 1 for t in times: # Open the NetCDF file ncfile = Dataset("/data/scratch/a/jtrapp/H_CTRL/cm1out_"+t+".nc") # MW runs: #ncfile = Dataset("/data/relamp/a/mjwoods3/bluewaters_cm1/May_20th/control_wrf/cm1out_"+t+".nc") ncfile['vmove'][0] dbz = ncfile['dbz'][0,32,:,:] u = ncfile['uinterp'][0,level,:,:] v = ncfile['vinterp'][0,level,:,:] UH = ncfile['uh'][0,:,:] UH2 = np.where(UH < 100, 0, UH) x = ncfile['xh'][:] y = ncfile['yh'][:] X,Y = np.meshgrid(x[0::3],y[0::3]) clevs = np.arange(10,75,1) plt.figure(figsize=(8,8), dpi=500) # 直接使用PyART内置色标 cf = plt.contourf(x, y, dbz, levels=clevs, cmap=pyart.graph.cm.HomeyerRainbow) #cf2 = plt.contour(x, y, UH, linewidths=0.75, alpha=0.6) #cf2 = plt.contour(x, y, UH2, levels=[-1,100,500], colors=['white','black','red'], linewidths=0.75, alpha=0.75) # plt.quiver(X,Y,u[0::3,0::3],v[0::3,0::3], scale=500, headwidth=10, headlength=10, headaxislength=5, width=0.0005, pivot='middle') plt.title('Simulated Reflectivity at '+str(t)+'s (1.25 km AGL)', fontsize=14, pad=3) plt.xlabel('X-distance (km)', fontsize=8) plt.ylabel('Y-distance (km)', fontsize=8) plt.colorbar(cf,fraction=0.05, pad=0.02, label='dBZ') # Display the plot plt.savefig('/data/scratch/a/jtrapp/ref'+t+".pdf", bbox_inches='tight')
内容的提问来源于stack exchange,提问作者kjmw
相关产品推荐
相关产品推荐

