How to specify the colors within a colorbar
80 views (last 30 days)
Show older comments
Caroline Whyatt
on 24 Apr 2017
Commented: Caroline Whyatt
on 25 Apr 2017
Hello all,
I am having problems setting the face color of my colorbar.
I have a range of data points color coded based on an underlying variable (example below is based on points ranging between Red - Yellow). While I can specify the value limits of the color bar, I am having trouble specifying the exact face color to be displayed on the color bar.
I have tried to directly edit within the color bar editor - but to no avail.
Any help would be much appreciated.

2 Comments
Image Analyst
on 24 Apr 2017
You've not given us much to go on. So just make up an N by 3 array of values in the range 0-1 and use the colormap() and colorbar functions. You might also use caxis(). A more descriptive question may get you a better answer.
Accepted Answer
David Goodmanson
on 25 Apr 2017
Edited: David Goodmanson
on 25 Apr 2017
Hi Carolyn, All you need do is change the colormap line to
colormap(sort(Color, 'descend'));
but make sure that you have cleared your variables first. With your previous command, Matlab creates a variable named 'colormap' and you do not want that.
The forgoing method works, but you get a pretty ugly colorbar with four sections of constant color. For a nicer colorbar you can make a more continuous version of the colormap, such as
x=[1,2,3,4];
y=[5,6,7,8];
sz = 100;
N = 64; % or whatever
Color1 = [ones(N,1),(N-1:-1:0)'/(N-1),zeros(N,1)];
colormap(Color1)
s=scatter(x,y,sz,y,'filled');
colorbar
This also gives you the opportunity to put not a matrix of rgb values into the scatter 'C' input as you did, but but rather to make C a vector of the same length as x and y. Then the collection of C vector values is mapped into the entire colormap from bottom to top. As you see, the y values were mapped to colors in the code above. That idea may not apply directly in your case with the circular plot, but you can make a C vector with appropriate values that map into the colormap.
More Answers (0)
See Also
Categories
Find more on Color and Styling 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!