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.
Show older comments
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
More Answers (1)
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
aditya deshmukh
on 2 Jan 2018
KSSV
on 2 Jan 2018
In the variable bin if you have 0 and 5, it will be replaced with nan. Your color range requirement is from 0 to 4.
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
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
Categories
Find more on Colorbar 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!