颜色图三种颜色
Colormap three colors
我有一个包含正电流和负电流(分别为上升流和下降流)的 netcdf 文件。我想创建一个 contourf,其中下降流为绿色,上升流为红色,0 为黑色。到目前为止,这是我的代码,包括来自 Mathworks 网站的一些代码 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: generating a colormap given three colors but I don't understand how you set the colors and it creates something similar to the above picture. The following has nothing to do with my question although it has the same title 。
预先感谢您的任何回答。
问题不在于颜色图错误,而在于数据值映射到颜色图的方式。这是通过将基础 axes
对象的 CLim
属性 更改为相对于零对称的对象来实现的,例如
set(gca,'CLim',[-1e-2 1e-2])
这也可以通过 UI 交互更改:Edit -> Colormap... 在图形菜单中.然后编辑颜色数据min/max.
我有一个包含正电流和负电流(分别为上升流和下降流)的 netcdf 文件。我想创建一个 contourf,其中下降流为绿色,上升流为红色,0 为黑色。到目前为止,这是我的代码,包括来自 Mathworks 网站的一些代码 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: generating a colormap given three colors but I don't understand how you set the colors and it creates something similar to the above picture. The following has nothing to do with my question although it has the same title
问题不在于颜色图错误,而在于数据值映射到颜色图的方式。这是通过将基础 axes
对象的 CLim
属性 更改为相对于零对称的对象来实现的,例如
set(gca,'CLim',[-1e-2 1e-2])
这也可以通过 UI 交互更改:Edit -> Colormap... 在图形菜单中.然后编辑颜色数据min/max.