Superimposing two imagesc graphs over each other.

220 views (last 30 days)
I have a matrix P which varies with time t,
P(:, :, t) ;
a variable Pmax,
Pmax = max(max(P(;,:,t)));
which I visualise through imagesc, step by step to create an animation:
figure
for i = 1:10:1000
imagesc(P(:, :, i), [0, Pmax]);
axis square
colorbar
end
I have another variable B, which I would like to visualise superimposed over the first imagesc plot, in a different colorbar/colour grading as P.
How could this be achieved? I was thinking of using a surf plot viewed from above instead (the two plots would be 2D, and the view set directly above to seem as a 2D plot). However to achieve this through the imagesc command would be much better.
Please share any ideas below, thank you!
  3 Comments
Shashvat Verma
Shashvat Verma on 20 Aug 2019
I was hoping to have the two images at 50 % transparency.
I wouldn't know how to map the images to the colormap manually, or how to put another set of axis over the other, if you could point me to the right direction with that?
The axis are the same for both images I am trying to overlap.
Thank you
Adam
Adam on 20 Aug 2019
How to map data onto a colourmap depends on the data and whether it is continuous or discrete. If you have an integer image, for example uint8, then something like e.g.
a = uint8( magic( 10 ) );
cmap = jet( 100 );
figure; imagesc( a );
colormap( cmap );
res = reshape( cmap( a, : ), [size(a) 3] );
figure; imagesc( res );
They should show the same image, but one uses the colourmap (and thus can be manipulated by instructions such as caxis) while the other is hard-mapped to RGB so is 3d.
If your data is floating point then you have to bin it to apply the colourmap. You can create a colourmap of whatever type and size you want, then bin your data into that many bins via histogram-based functions.
Likewise if you want the range to be different than the default you have to do more manipulations when mapping the data onto the colourmap (e.g. the above example only uses values 1 to 100 and these are mapped over the full colourmap range).
Transparency is applied by setting the 'AlphaData' property with imagesc, in your case you would set it to a scalar 0.5.
This will make the top image have 50% transparency. I doubt you want the bottom image to also have 50% transparency as this wouldn't make much sense with nothing underneath it.

Sign in to comment.

Accepted Answer

Subhadeep Koley
Subhadeep Koley on 22 Aug 2019
It is difficult to give exact solution without the exact data. But, below is an example of how you can superimpose 2 data using imagesc with different transparency & colormap.
%plot first data
ax1 = axes;
im = imagesc(ax1,imread('cameraman_512.png'));
im.AlphaData = 0.5; % change this value to change the background image transparency
axis square;
hold all;
%plot second data
ax2 = axes;
im1 = imagesc(ax2,imread('fighter.png'));
im1.AlphaData = 0.5; % change this value to change the foreground image transparency
axis square;
%link axes
linkaxes([ax1,ax2])
%%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
%add differenct colormap to different data if you wish
colormap(ax1,'summer')
colormap(ax2,'winter')
%set the axes and colorbar position
set([ax1,ax2],'Position',[.17 .11 .685 .815]);
cb1 = colorbar(ax1,'Position',[.05 .11 .0675 .815]);
cb2 = colorbar(ax2,'Position',[.88 .11 .0675 .815]);
cameraman_512.png
fighter.png
overlap.png

More Answers (0)

Community Treasure Hunt

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

Start Hunting!