Specify a color from a relative range of data

조회 수: 3 (최근 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 CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by