Specify a color from a relative range of data

6 ビュー (過去 30 日間)
amatuercoder
amatuercoder 2016 年 2 月 2 日
回答済み: Walter Roberson 2016 年 2 月 3 日
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.
  2 件のコメント
amatuercoder
amatuercoder 2016 年 2 月 2 日
Worked like a charm! Thanks a lot Brendan
Brendan Hamm
Brendan Hamm 2016 年 2 月 3 日
If this answered your question, please formally Accept my answer.

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by