Plot a 2-dimensional matrix with 2 different colormaps for negative and positive values
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
THONTI BEERAIAH
am 25 Aug. 2023
Kommentiert: Dyuman Joshi
am 28 Aug. 2023
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).![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1464662/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1464662/image.png)
0 Kommentare
Akzeptierte Antwort
Dyuman Joshi
am 25 Aug. 2023
Bearbeitet: Dyuman Joshi
am 25 Aug. 2023
%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 Kommentare
Dyuman Joshi
am 28 Aug. 2023
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)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Colormaps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!