我有一个包含正电流和负电流(分别为上升流和下降流)的 netcdf 文件。我想创建一个轮廓,其中下降流为绿色,上升流为红色,0 为黑色。到目前为止,这是我的代码,包括 Mathworks 网站上的一些代码https://nl.mathworks.com/matlabcentral/answers/81352-colormap-with-both-positive-and-negative-values https://nl.mathworks.com/matlabcentral/answers/81352-colormap-with-both-positive-and-negative-values :
%open netcdf file
ncdisp('20110810_061103.nc');
ncdisp('grid.nc');
latu=ncread('grid.nc','latitude_u');
lonu=ncread('grid.nc','longitude_u');
latv=ncread('grid.nc','latitude_v');
lonv=ncread('grid.nc','longitude_v');
u = ncread('20110810_061103.nc','vel_u'); %x axes velocity of water
v = ncread('20110810_061103.nc','vel_v');%y axes
w = ncread('20110810_061103.nc','w');%z axes
Minu=min(min(min(u)))
Minv=min(min(min(v)))
Minw=min(min(min(w)))
Maxu=max(max(max(u)))
Maxv=max(max(max(v)))
Maxw=max(max(max(w)))
figure
contourf(lonu(1:681,1:711),latu(1:681,1:711),w(1:681,1:711,20))
%code I copied from mathworks
greenColorMap = [zeros(1, 132), linspace(0, 1, 124)];
redColorMap = [linspace(1, 0, 124), zeros(1, 132)];
colorMap = [redColorMap; greenColorMap; zeros(1, 256)]';
% Apply the colormap.
colormap(colorMap);
colorbar
正如您所看到的,黑色不为 0。如何确保黑色为零,下降流 (-) 为红色,上升流 (+) 为绿色?如果这是重复的,我很抱歉。我检查了问题MATLAB:生成给定三种颜色的颜色图 https://stackoverflow.com/questions/17279298/matlab-generating-a-colormap-given-three-colors但我不明白你如何设置颜色,它会创建类似于上图的内容。以下内容与我的问题无关,尽管它具有相同的标题Matplotlib:具有三种颜色的自定义颜色图 https://stackoverflow.com/questions/32952963/matplotlib-custom-colormap-with-three-colors。
预先感谢您的任何答复。