Matrix values represented as colormap

Hi everyone,
I am trying to represent the values of a matrix as a colormpap. The problem I am currently having is that this matrix does not have any negative values and I'd like my "colorbar" to represent this as well without altering the way the colormap currelty looks. I'd like my color bar to go from 0 to max(E_superimposed) but when I chnage the "caxis" the colormap gets all distorted. This is the image I am getting from my code: Thank you so much in advance
figure(3)
colormap(flipud(colormap));
set(gcf, 'PaperPosition', [0 0 8 5]);
set(gcf, 'PaperSize', [8 5]);
imagesc(E_superimposed);
caxis([-max(E_superimposed(:)) max(E_superimposed(:))])
colorbar
colormap(lbmap(201,'RedWhiteBlue'))
set(gca,'XTick',[])
set(gca,'YTick',[])
title('Superimposed Block Matrix')

 Accepted Answer

Why do
caxis([-max(E_superimposed(:)) max(E_superimposed(:))])
instead of
caxis([min(E_superimposed(:)) max(E_superimposed(:))])

6 Comments

I tried that and and completely changed the colormap, it turned it all red
DGM
DGM on 25 May 2021
Edited: DGM on 25 May 2021
I think I see what you're asking for. Let me see if I'm right. You want the image displayed as it is (from white to blue), and you want the colorbar to just go from white to blue as well, instead of from red to white to blue?
If that's the case, you can just truncate the colormap.
cm = lbmap(201,'RedWhiteBlue');
colormap(cm(115:end,:)) % just select the part of the colormap you want
The version of lbmap() I have doesn't have a map called RedWhiteBlue, so you might need to tweak the breakpoint. You can alternatively just edit it using colormapeditor().
My version of lbmap() has a map called 'Blue' which is basically the same, so you could just use that instead if you have something similar.
Hi DGM, yes what you said is exactly what I want. but the the matrix keeps changing everytime I try to change the colorbar. Ideally, I'd like to keep the matrix looking the exact sameway with only the colorbar bar displaying 0 t0 10 values instead of negative since I have none. Thank you
DGM
DGM on 25 May 2021
Edited: DGM on 25 May 2021
By default, imagesc() scales the output such that the first entry in the color table corresponds to min(data) and the last entry in the color table corresponds to max(data).
If you change the colormap to one that extends from white to blue, you'll have to get rid of this line
% this forces the image to only use half the colormap
% if the colormap is the right size, you don't want this
caxis([-max(E_superimposed(:)) max(E_superimposed(:))])
Just let imagesc do the scaling and it will span from white to blue as it does now.
For demonstration, this is how you started:
% just a random test image (spans [3 44])
x = 1:20;
A = x+x.'+5*rand(20);
imagesc(A)
colorbar
% ignore the difference btw 'RedBlue' and 'RedWhiteBlue'
colormap(lbmap(201,'RedBlue'))
caxis([-max(A(:)) max(A(:))])
If you use a colormap that just spans from white to blue (either by truncating the RedBlue map as I mentioned or using a different Blue map), you lose all the white parts of the image because the caxis() line is still forcing it to only use half the colormap.
% just a random test image (spans [3 44])
x = 1:20;
A = x+x.'+5*rand(20);
imagesc(A)
colorbar
colormap(lbmap(201,'Blue'))
caxis([-max(A(:)) max(A(:))])
So simply don't use caxis() and let imagesc() do the scaling.
% just a random test image (spans [3 44])
x = 1:20;
A = x+x.'+5*rand(20);
imagesc(A)
colorbar
colormap(lbmap(201,'Blue'))
Or if you want to explicitly specify the limits (e.g. if the data doesn't quite go to zero like this case)
% just a random test image (spans [3 44])
x = 1:20;
A = x+x.'+5*rand(20);
imagesc(A)
colorbar
colormap(lbmap(201,'Blue'))
caxis([0 max(A(:))])
hi DGM, yes thats exactly what I want but I cant get "Blue" to work, how did you modify it?
In the above example, I simply used this function
I don't know how that differs from your version of lbmap().

Sign in to comment.

More Answers (1)

Try
imagesc(E_superimposed);
caxis([0, max(E_superimposed(:))]) % Ignore negative values.
or maybe
cmap = lbmap(201,'RedWhiteBlue')
imshow(E_superimposed, [], 'Colormap', cmap); % Don't use caxis after this.

Tags

Asked:

on 25 May 2021

Commented:

DGM
on 26 May 2021

Community Treasure Hunt

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

Start Hunting!