필터 지우기
필터 지우기

Plot a 2-dimensional matrix with 2 different colormaps for negative and positive values

조회 수: 3 (최근 30일)
I have a big matrix consisting of values ranging from negative to positive. When I plot that by just imagesc and apply colorbar it is shpwoing gradiant over all. My requirement is to apply one colormap for negative values (probably cool) and the other colormap for positive values (probabliy hot) in a same plot as attached ( so there will be clear difference between negative and positive values).

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 8월 25일
편집: Dyuman Joshi 2023년 8월 25일
%Random data for example
y = (-7:5)+(-7:5)';
%Get the maximum and minimum values of the array
p = max(y,[],'all');
n = min(y,[],'all');
%Display via imagesc
imagesc(y)
%Two colormaps
col = [cool(abs(n));hot(p)];
%Multiply the input with the same number to get more divisions on colors
%col = [cool(3*abs(n));hot(3*p)];
cb = colorbar('Ticks',n:p);
colormap(col)
  댓글 수: 3
THONTI BEERAIAH
THONTI BEERAIAH 2023년 8월 28일
Thank you, however you solution worked nice for matrix with integers. I have a matrix which has noninteger values where the method is not working. I am uploading one matrix (as text file) so could you please do the same for the uploaded data?
Dyuman Joshi
Dyuman Joshi 2023년 8월 28일
For non-integer values, round the values away from zero -
%Random data for example
vec = (-7.5:0.5:5);
y = vec+vec';
%Get the maximum and minimum values of the array
p = max(y,[],'all');
n = min(y,[],'all');
%As the input to colormaps must be positive (because it's an index)
%to round away from zero, we take ceil() of the absolute value
f = @(x) ceil(abs(x));
%Display via imagesc
imagesc(y)
%Two colormaps
col = [cool(f(n));hot(f(p))];
cb = colorbar('Ticks',n:p);
colormap(col)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by