How to specify the colors within a colorbar

80 views (last 30 days)
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
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.
Caroline Whyatt
Caroline Whyatt on 24 Apr 2017
Thank you Image Analyst. I have a N x 3 color array that specifies the face color of each data point. Unfortunately, I am seem to be having problems specifying the face color of the associated Color Bar.
For example,
x=[1,2,3,4];
y=[5,6,7,8];
sz=[100,100,100,100];
Color=[1,1,0;
1,0.666666666666667,0;
1,0.333333333333333,0;
1,0,0];
s=scatter(x,y,sz, Color, 'filled');
colormap=(sort(Color, 'descend'));
set(gca, 'CLim', [1, 8]);
c=colorbar;
Results in the image below. The color bar axis/range is set, but I am trying to find a way to specify the face color of the color bar - away from the default - so that it only depicts the range of colors in the chart. Apologies in advance if this is a silly oversight!
Thank you again for your help.

Sign in to comment.

Accepted Answer

David Goodmanson
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.
  1 Comment
Caroline Whyatt
Caroline Whyatt on 25 Apr 2017
Hi David, thank you for noticing that oversight! The blending method that you kindly provided works very well - currently trying to adapt it to the circular plot. Many thanks!

Sign in to comment.

More Answers (0)

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!