我已按照离散间隔颜色栏example改编自NCL
的自定义色阶。我的数据数组位于NetCDF文件中,当我尝试对其进行绘制时,该图和颜色栏看起来像pcolormesh
图所期望的那样,但是当我绘制contourf
图时,它变得很疯狂。 定义色阶时我做错什么了吗?
import matplotlib.colors
import xarray as xr
# loading data
ds = xr.open_dataset(path/tofile/data.nc)
## custom color bar
## converting RGB values to 0,1
cmap3 = matplotlib.colors.ListedColormap([(127/255, 150/255, 255/255), (142/255, 178/255, 255/255), (181/255, 201/255, 255/255), (214/255, 226/255, 237/255),\
(242/255, 221/255, 160/255), (242/255, 132/255, 68/255), (229/255, 0/255, 0/255), (1, 163/255, 10/255), (1, 249/255, 20/255), (172/255, 206/255, 100/255), (125/255, 190/255, 84/255)])
# I want to discretize the colorbar according to values
vals = [-1.0, 0.0, 0.2 ,0.5, 0.8, 1.0, 1.5, 2.0, 4.0 , 6.0 , 8.0, 10]
norm = matplotlib.colors.BoundaryNorm(vals, cmap3.N)
# variable to be plotted
ds.PV.squeeze().plot.pcolormesh(cmap=cmap3, norm=norm, vmin=0, vmax=10)
# contourf plot which is giving unexpected result
ds.PV.squeeze().plot.contourf(cmap=cmap3, norm=norm, vmin=0, vmax=10)
带有Pcolormesh图的预期色标:
这是我的正确颜色条的外观:
fig, ax =plt.subplots(figsize=(6,1))
cb3 = matplotlib.colorbar.ColorbarBase(ax, cmap=cmap3,
norm=norm,
extend='neither',
ticks=vals,
spacing='uniform',
orientation='horizontal')
答案 0 :(得分:1)
@ImportanceofBeingErnest this详细的答案有所帮助。在contourf
中,必须特别指定需要绘制轮廓的级别,而pcolormesh
似乎是根据颜色条自动选择的。
ds.PV.squeeze().plot.contourf(cmap=cmap3, norm=norm,levels=vals)