how to set a limit in each color in a color bar?

23 views (last 30 days)
Hi! So I want a color bar with only three colors (no gradient). I did:
A = [1; 2; 3; 4]; B = [10; 20; 30; 40]; C = [5; 10; 20; 30]; pointsize = 50;
scatter(A,B, pointsize, C);
colormap([0 1 0; 1 1 0; 1 0 0]) % apply colormap - three colors
caxis([0, 50]) %limits of the color bar
colorbar % show color bar
However, I want the colors to have specific intervals. Like: - green between 0 and 9 - yellow between 10 and 40 - red between 41 and 50
Can someone help me? Thank you!!

Answers (1)

alice
alice on 4 Jul 2017
You only use three colors, so you can change caxis value in order to have the values you want at the limit because the colors, knowing that it will create 3 equal length colors. Then change the limits of the axe. So you can add to your code:
limitGreenYellow = 9.5;
limitYellowRed = 40.5;
caxis([2*limitGreenYellow-limitYellowRed, 2*limitYellowRed-limitGreenYellow]) %limits of the color bar
c=colorbar; % show color bar
xlim(c,[min(limitValues),max(limitValues)]);
Another (better?) way to get what you want would be to have a huge colormap, 1 row for every integer, and it would work for more than 3 colors:
limits = [0,9,10,40,41,50];
colormap([repmat([0 1 0],limits(2)-limits(1)+1,1); repmat([1 1 0],limits(4)-limits(3)+1,1); repmat([1 0 0],limits(6)-limits(5)+1,1)]);
caxis([0, 50]) %limits of the color bar
  1 Comment
Melissa Buechlein
Melissa Buechlein on 16 Nov 2017
Thank you for posting this code, it is very helpful. I want to do a similar thing, but my break points are:
[0 0.33 1 2.5 5 11 23 50 120];
I tried multiplying everything by 100, but the breaks don't line up with where I want them. Also, I have several break points that appear the same color.
Here is the code I am using:
limits = [0 33 34 100 101 2500 2501 500 501 1100 1101 2300 2301 3500 3501 5000 5001 12000];
colormap([repmat([0.94 0.94 0.94],limits(2)-limits(1),1);...
repmat([0.9 0.9 0.9],limits(4)-limits(3)+1,1);...
repmat([0.5 0.5 0.5],limits(6)-limits(5)+1,1);...
repmat([0.1 0.1 0.1],limits(8)-limits(7)+1,1);...
repmat([0 1 1],limits(10)-limits(9)+1,1);...
repmat([0 0 1], limits(12)-limits(11)+1,1);...
repmat([1 1 0], limits(14)-limits(13)+1,1);...
repmat([1 0.7 0], limits(16)-limits(15)+1,1);...
repmat([1 0 0], limits(18)-limits(17)+1,1)]);
Please advise!

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!