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.

조회 수: 12 (최근 30일)
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.

채택된 답변

KSSV
KSSV 2018년 1월 2일
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
Constantino
Constantino 2018년 11월 9일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 1월 2일
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
Walter Roberson
Walter Roberson 2018년 1월 2일
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 2018년 8월 4일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by