How can I overlay pseudocolor images with different colormaps?

52 views (last 30 days)
Using Matlab R2015b with OpenGL rendering, I am trying to superimpose two pseudocolor images associated with different colormaps. Both images correspond to matrices displayed using imagesc. The commands are the following:
% Examples of matrices x=linspace(-7,7,200);y=x;[X,Y]=meshgrid(x,y);Z1=(X.^2+Y.^2).*exp(-(X.^2+Y.^2)/3^2);Z2=exp(-X.^2-Y.^2);
% New figure
% Background image
h1 = axes;colormap(h1,'parula');p1=imagesc(x,y,Z1);set(h1,'ydir','normal');
% Foreground image
h2=axes;set(h2,'color','none','visible','off');colormap(h2,'jet');p2=imagesc(x,y,Z2,'alphadata',Z2>1e-2);set(h2,'ydir','normal');linkaxes([h1 h2])
I would like to display only the central part of the (Gaussian) matrix Z2, hence the transparency applied to its wings (with values < 1e-2). Doing so, the foreground image indeed gets paler, yet remains opaque to the background image. Actually, it remains opaque to the latter even by setting full transparency, i.e., set(p2,'alphadata',0)!
Consequently, I wonder whether it is possible at all to overlay pseudocolor images.
Thank you for your help.

Accepted Answer

Mike Garrity
Mike Garrity on 9 Mar 2016
Edited: Mike Garrity on 9 Mar 2016
You actually have it exactly right, but the second call to imagesc reset the axes h2. This set the Visible property back to on. Just move this line:
Down to where you're doing this:
As long as an axes hold state is off, a plotting command will reset its properties. This means that changing properties on the axes before the plotting command doesn't work very well.

More Answers (2)

Isaac Breinyn
Isaac Breinyn on 16 Jun 2019
For anyone else who remained confused for a moment, one must also move the line
colormap(h2, 'jet');
after imagesc, so that the working example is:
% Examples of matrices
% New figure
% Background image
h1 = axes;colormap(h1,'parula');
% Foreground image
linkaxes([h1 h2])

Laurent G
Laurent G on 9 Mar 2016
It works nicely. Thanks a lot.


Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by