Specify a color from a relative range of data

19 views (last 30 days)
Hi, i am trying to specify a color (RGB values) from a known value between a min and a max. Let me elaborate...I have a column of data (Fn) with a minimum value (Fnmin) and maximum value (Fnmax). How can set Fnmin equal to blue [0 0 1], Fnmax equal to red [1 0 0], and then have the code provide the relative RGB values for an Fn value that falls between Fnmin and Fnmax?

Accepted Answer

Brendan Hamm
Brendan Hamm on 2 Feb 2016
Since your colors are really just numeric values, we can perform an interpolation on those numbers using interp1:
b = [0 0 1]; % Blue
r = [1 0 0]; % Red
x = 0:0.01:1; % Values to interpolate on (This would be your actual vector of values)
xOk = [min(x);max(x)];
% Interpolate over all of the values in x
y = interp1(xOk,[b;r],x); % Here Blue corresponds to xOk == 0 and Red to xOk == 1
scatter(x,x,[],y) % Scatter x vs. x but color based on the interpolated color.
  2 Comments
amatuercoder
amatuercoder on 2 Feb 2016
Worked like a charm! Thanks a lot Brendan
Brendan Hamm
Brendan Hamm on 3 Feb 2016
If this answered your question, please formally Accept my answer.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 3 Feb 2016
Alternative solution: colormap() your desired colormap into place and then
caxis([Fnmin Fnmax])
This says that Fnmin is to be mapped to the first color, Fnmax is to be mapped to the last color, and everything in between to be mapped proportionally to the value and number of colormap entries.

Community Treasure Hunt

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

Start Hunting!