How to customize a colorbar of contourf plot such that different colors are for values in different ranges? The interval in the range may or may not be constant.

11 views (last 30 days)
I want to have a contourf plot such that it is giving one particular color for values from 0-1, another from 1-1.25, 1.25-1.75 and 1.75-4. The interval of the range is not constant. Is is possible in MATLAB?. I would appreciate some solution for this problem.

Accepted Answer

KSSV
KSSV on 2 Jan 2018
Let z be your data to plot a contour map. And crange be the colors range you want to have.
crange = [ 0 1;1 1.25;1.25 1.75 ; 1.75 4 ; 4 inf] ;
cmap = jet(size(crange,1)) ;
%%Arrange the colors range
colors = zeros(size(z));
for i = 1:size(crange,1)
colors(z > crange(i,1) & z<=crange(i,2)) = crange(i,2);
end
Now use contour/ surf with z. and colormap(cmap); colorbar ;
  2 Comments
Constantino
Constantino on 9 Nov 2018
If I understand right, you are mapping the original matrix Z into another one named colors with all values in each color region set to the upper limit of the range, am I right? So you should plot colors and not Z? Sorry my misunderstanding but Im still not familiar with the sintax
colors(z > crange(i,1) & z<=crange(i,2)) = crange(i,2);

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 2 Jan 2018
cmap = hsv(4); %or other colormap with 4 entries
[~, bin] = histc(DataValues, [-inf 0 1 1.25 1.75 4*(1+10*eps) inf]);
bin = bin - 1;
bin( ismember(bin, [0 5]) ) = nan;
image(bin)
colormap(cmap)
  4 Comments
Walter Roberson
Walter Roberson on 2 Jan 2018
You do not define colors for the range -inf to 0, or for the range 4 to inf, so we have to set those area to be transparent, which we do by setting the data value to nan.
histc would return bin number of 1 for the -inf to 0 range (the first bin), and would return 6 for the range from 4*(1+10*eps) to inf. But in the next line we subtract 1 from all of the bin numbers, so numbers 1 and 6 move to become 0 and 5. We set them to be nan for transparent.
Now we have an array that is nan where the data was outside the defined range, and is 1 for the range 0 <= data < 1, and is 2 for the range 1 <= data < 1.25, and is 3 for the range 1.25 <= data < 1.75 and so on. Those data values are now suitable for color table entry numbers.
Bijay Guha
Bijay Guha on 4 Aug 2018
The code is fine. But I have a matrix the values of which ranges from ~0.000001 to ~0.004. I want to separate the colors like this.. 1e-6 to 2e-6 (one color), 2e-6 to 5e-6 (next color), then 5e-6 to 1e-5, 1e-5 to 2e-5, 2e-5 to 5e-5, 5e-5 to 1e-4, 1e-4 to 2e-4, 2e-4 to 5e-4, 5e-4 to 1e-3, 1e-3 to 2e-3, 2e-3 to 3e-3, then 3e-3 to higher values. Here, 1e-6 = 0.000001. Somehow I managed to give different range of values to different colors, but the whole discrete set of ranges is not showed up in the caxis. It only showed discrete range in caxis for values higher than 0.0005, values below that, i.e. 0 to 0.0005 shown only by a single color. Similar to the attached picture is what I want..Could you please help..?? Thanks in advance

Sign in to comment.

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!