Specify a color from a relative range of data
13 views (last 30 days)
Show older comments
amatuercoder
on 2 Feb 2016
Answered: Walter Roberson
on 3 Feb 2016
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?
0 Comments
Accepted Answer
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
More Answers (1)
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.
0 Comments
See Also
Categories
Find more on Blue 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!