How to perfectly superimpose 2 dataset as images

4 次查看(过去 30 天)
Dear all,
I am struggleling a bit. I have 2 dataset both of 81x161 cell. The first one is a mask (contains only 0 and 1 value) for sample detection. The other one (M) contain NAN values when there were no analysed signal and variable between -10 and +10 when a signal was analysed.I want to make a figure with the mask as black and white and the data as jet colormap scale to my min and max values.
I have an issue, I have a very small shift of a few pixel in between the 2 images which is created when I add the colorbar. Without everything is fine, but I would need the colorbar...
May someone help me please ?
Here is my code :
fig2=figure('color','white','visible','on','units','centimeters','outerposition',[5 5 20 20]); % white background, defined position and size
%background image
ax1=axes('Parent',fig2);%define the axis so background and foreground can have different colormap
image(ax1,Mask,'CDataMapping','scaled');%transform matrix into image and scale the color to min/max values
colormap(ax1,mymap);% sets the B/W colormap for the figure
set(ax1,'visible','off')%remove the axis from ax1
set(ax1, 'Color', 'white')% white background
ax1.XLim = [0 161];
ax1.YLim = [0 81];
hold on;
%foreground image
ax2=axes('Parent',fig2);%define the axis so background and foreground can have different colormap
I=image(ax2,M,'CDataMapping','scaled');%transform matrix into image and scale the color to min/max values
set(I, 'AlphaData', ~isnan(M));%put all Not-a-number as white and exclude from scalebar
colormap(ax2,'jet');% sets the colormap for the figure
set(ax2, 'Color', 'None')%remove the background
ax2.XLim = [0 161];
ax2.YLim = [0 81];
linkaxes([ax1 ax2],'xy');%link the 2 axes so they superimpose
colorbar ('AxisLocation', 'out' ); %add the colobar outside of the axe

回答(1 个)

prabhat kumar sharma
Hello Elodie,
It understand that you're trying to overlay two images with different colormaps and then add a colorbar. The issue with the shift when adding the colorbar is likely due to the axes resizing to accommodate the colorbar. This can be fixed by ensuring the axes positions are explicitly set and maintained.
% Ensure the axes positions are explicitly set and maintained
ax1.Position = [0.1, 0.1, 0.8, 0.8];
ax2.Position = ax1.Position;
Axes Position - By explicitly setting the Position property of both axes (ax1 and ax2), you ensure that adding the colorbar does not cause the axes to shift. The Position property is set to [0.1, 0.1, 0.8, 0.8], which you can adjust as needed.
I hope it helps!

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by