Overlay two images in subplot with different colormaps
Mostrar comentarios más antiguos
Hi,
I want to overlay an image (in colour) and its gradient (in gray) in a subplot. I am trying to follow the response to this question: Overlay imagesc and contour in subplot with different colour bars
The code below works without subplot: im1 is the orignal image and im2 is its gradient. When I use a subplot, however, the colormap gray used for the second image is also applied to the first image. That seems to be related to the definition of axes for im2, but I can not find what it should be. Could you help me with that ?
Best,
Didier
Z = peaks(101);
[dZdx, dZdz] = gradient(Z);
gradZ = abs(dZdx.^2 + dZdz.^2);
%% WIthout subplot, it works
figure()
%plot first data
ax1 = axes;
im1 = imagesc(ax1,Z);
im1.AlphaData = 1;
axis equal;
hold all;
set(gca,'YDir','normal')
colormap(ax1,jet)
%plot second data
ax2 = axes;
im2 = imagesc(ax2,gradZ);
im2.AlphaData = gradZ/max(max(gradZ)); % change this value to change the foreground image transparency
axis equal;
hold all;
set(gca,'YDir','normal')
%link axes
%%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
colormap(ax2,gray)
ax2.UserData = linkprop([ax1,ax2],...
{'Position','InnerPosition','DataAspectRatio','xtick','ytick', ...
'ydir','xdir','xlim','ylim'});
%% WIth a subplot, the colormap gray is applied also to the first image 'im1'
fig=clf()
h = subplot(211);
%plot first data
ax1 = h;
im1 = imagesc(ax1,Z);
im1.AlphaData = 1; % change this value to change the background image transparency
axis equal;
hold all;
set(gca,'YDir','normal')
colormap(ax1,jet)
%plot second data
ax2 = h;
im2 = imagesc(ax2,gradZ);
im2.AlphaData = gradZ/max(max(gradZ)); % change this value to change the foreground image transparency
axis equal;
hold all;
set(gca,'YDir','normal')
%link axes
%%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
colormap(ax2,gray)
ax2.UserData = linkprop([ax1,ax2],...
{'Position','InnerPosition','DataAspectRatio','xtick','ytick', ...
'ydir','xdir','xlim','ylim'});
Respuestas (1)
This happens because when you do it without subplot, you are making two images in two separate axes, but when you use subplot, the two images are going into the same axes.
Your code with figures:
Z = peaks(101);
[dZdx, dZdz] = gradient(Z);
gradZ = abs(dZdx.^2 + dZdz.^2);
%% WIthout subplot, it works
figure()
%plot first data
ax1 = axes;
im1 = imagesc(ax1,Z);
im1.AlphaData = 1;
axis equal;
hold all;
set(gca,'YDir','normal')
colormap(ax1,jet)
%plot second data
ax2 = axes;
im2 = imagesc(ax2,gradZ);
im2.AlphaData = gradZ/max(max(gradZ)); % change this value to change the foreground image transparency
axis equal;
hold all;
set(gca,'YDir','normal')
%link axes
%%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
colormap(ax2,gray)
ax2.UserData = linkprop([ax1,ax2],...
{'Position','InnerPosition','DataAspectRatio','xtick','ytick', ...
'ydir','xdir','xlim','ylim'});
%% WIth a subplot, the colormap gray is applied also to the first image 'im1'
fig=clf()
h = subplot(211);
%plot first data
ax1 = h;
im1 = imagesc(ax1,Z);
im1.AlphaData = 1; % change this value to change the background image transparency
axis equal;
hold all;
set(gca,'YDir','normal')
colormap(ax1,jet)
%plot second data
ax2 = h;
im2 = imagesc(ax2,gradZ);
im2.AlphaData = gradZ/max(max(gradZ)); % change this value to change the foreground image transparency
axis equal;
hold all;
set(gca,'YDir','normal')
%link axes
%%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
colormap(ax2,gray)
ax2.UserData = linkprop([ax1,ax2],...
{'Position','InnerPosition','DataAspectRatio','xtick','ytick', ...
'ydir','xdir','xlim','ylim'});
Modified code with figures (note the usage of copyobj to get two identical axes in the part with subplot):
Z = peaks(101);
[dZdx, dZdz] = gradient(Z);
gradZ = abs(dZdx.^2 + dZdz.^2);
% WIthout subplot, it works
figure()
%plot first data
ax1 = axes;
im1 = imagesc(ax1,Z);
im1.AlphaData = 1;
axis equal;
hold all;
set(gca,'YDir','normal')
colormap(ax1,jet)
%plot second data
ax2 = axes;
im2 = imagesc(ax2,gradZ);
im2.AlphaData = gradZ/max(max(gradZ)); % change this value to change the foreground image transparency
axis equal;
hold all;
set(gca,'YDir','normal')
%link axes
%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
colormap(ax2,gray)
ax2.UserData = linkprop([ax1,ax2],...
{'Position','InnerPosition','DataAspectRatio','xtick','ytick', ...
'ydir','xdir','xlim','ylim'});
% WIth a subplot, the colormap gray is applied also to the first image 'im1'
% fig=clf()
f = figure();
h = subplot(211);
%plot first data
ax1 = h;
ax2 = copyobj(ax1,f); % make a copy of ax1 before it has anything in it, to be used for im2
im1 = imagesc(ax1,Z);
im1.AlphaData = 1; % change this value to change the background image transparency
axis equal;
hold all;
set(gca,'YDir','normal')
colormap(ax1,jet)
%plot second data
% ax2 = h;
% h = subplot(211);
im2 = imagesc(ax2,gradZ);
im2.AlphaData = gradZ/max(max(gradZ)); % change this value to change the foreground image transparency
axis equal;
hold all;
set(gca,'YDir','normal')
%link axes
%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
colormap(ax2,gray)
ax2.UserData = linkprop([ax1,ax2],...
{'Position','InnerPosition','DataAspectRatio','xtick','ytick', ...
'ydir','xdir','xlim','ylim'});
There may be other things, but this is the crux of it, I think.
Categorías
Más información sobre Blue en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



