MATLAB: Change colors of pcolor meeting spesific conditions

pcolor

Hi there
I have a 810×810 elevation data matrix (le's say Z). I want to plot it using pcolor with
colormap(winter(10))
However, for every Z <= 0, I want to change the color into one ocean blue or just plain red. Does anyone know how to do this?
Thanks.
Abdul

Best Answer

cmap = winter(10));
cmap(1,:) = [.7 0 0]; %plain red
Ztemp = max(0, Z);
pcolor(X, Y, Ztemp);
colormap(cmap)
When you use pcolor(), the Z coordinates are all set to 0, not to the value of the data, so it does not matter that your Z is not the "correct" Z, you won't be able to tell with datatips (not unless you use a custom datatip function.)
There are alternatives to pcolor() that do not set the Z to all 0, and for those alternative functions, it can be a bit tricky to arrange that all negative values are one color without affecting the correct functioning of the Z coordinate in datatips.