MATLAB Answers

Superimposing two imagesc graphs over each other.

78 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:
for i = 1:10:1000
imagesc(P(:, :, i), [0, Pmax]);
axis square
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!


Adam on 19 Aug 2019
What are you expecting the result to be? If you plot one image on top of another then, in general, it will completely hide the first image, assuming it is at least as big. You would need to use some kind of alpha/transparency effect to be able to view both some kind of algorithm to actually combine them into a single image.
Since you want a different colourmap you would have to either map at least one image onto the colourmap manually to get an RGB image or have one axis on top of another so that you can have a different colourmap with each axis, but this again comes with issues of transparency and effects on picking, zooming, etc.
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 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
%%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
%add differenct colormap to different data if you wish
%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]);

  1 Comment

Saeed Mohammadian
Saeed Mohammadian on 9 Sep 2019
Thank you very much.
That worked, I appreciate your time

Sign in to comment.

More Answers (0)

Translated by