Colormap three colors

99封情书 提交于 2019-12-13 13:49:07

问题


I have a netcdf file with positive and negative currents (upwelling and downwelling respectively). I want to create a contourf where downwelling is green, upwelling is red and 0 is black. So far this is my code including some code from the Mathworks website 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

As you can see, the black is not at 0. How do I make sure that black is zero, downwelling (-) is red and upwelling(+) is green? I'm sorry if this is a duplicate. I checked question 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 Matplotlib: Custom colormap with three colors . Thank you in advance for any answers.


回答1:


The issue is not with the colormap being wrong, but with the way the data values are mapped to the colormap. This is achieved by changing the CLim property of the underlying axes object with something symmetric with respect to zero, e.g. set(gca,'CLim',[-1e-2 1e-2])

This can also be changed interactively through the UI: Edit -> Colormap... in the figure menu. Then edit color data min/max.



来源:https://stackoverflow.com/questions/53763218/colormap-three-colors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!