0
votes

In Matlab consider a MxN matrix A with values in [-o p] with M,N as integer values and o,p as double values. The number of values on either side of origo in M and N are not necessarily even.

A is visualised using Matlabs imagesc and colorbar.

How can I force colorbar to set values from A that are close to zero as a certain color?

Please note that I do not wish to (for example) 'hard code' zero (0) as green (0 128 0), but instead I wish to use the default rgb or hsv colormap and have it adjust to the values in A whilst still keeping zero (or close to zero) in green color tone.

2
Adjust the color limits which control the mapping of the colorbar to your dataSuever
I attempted this but was not able to map near-zero or zero values to a specific region of the gradient. How would you go about mapping say [-71 ... 0 ... 300] so that zero and values close to it are in the green section of the gradient?Peter
Apart from creating a custom colormap, you would need to do caxis([-300 300])Suever
Well, I posted a suggestion below but I feel as if it is extremely inefficient. There has to be a faster way to do such a trivial task I imagine. For example if the negative values in A are just slightly below zero and the positive values are in the 1000's or greater having to do a [-1000 1000] seem not efficient.Peter

2 Answers

2
votes

If you want the 0 value to actually be in the middle of your colormap, you will want to set your color limits (using caxis of the CLims property of the axes) after determining the maximum magnitude of your data:

limit = max(abs(data(:)));
caxis([-limit, limit]);
0
votes

This is what I am going with. I really feel that there is a faster/better way of doing this so please post improvements.

[minM minMlix] = min(oM(:));
[maxM maxMlix] = max(oM(:));
gr1 = abs( minM );
gr2 = abs (maxM );
if ( gr1 > gr2 )
    maxM = gr1;
elseif (gr1 < gr2 )
    minM = -maxM;
end
caxis([minM maxM])