Specify a color from a relative range of data

12 次查看(过去 30 天)
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?

采纳的回答

Brendan Hamm
Brendan Hamm 2016-2-2
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.

更多回答(1 个)

Walter Roberson
Walter Roberson 2016-2-3
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.

类别

Help CenterFile Exchange 中查找有关 Blue 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by