How to perfectly superimpose 2 dataset as images
조회 수: 1 (최근 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
댓글 수: 2
답변 (1개)
prabhat kumar sharma
2024년 7월 3일
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!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Colormaps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!