shamalaia
shamalaia

Reputation: 2351

align axes in one figure

I have a figure with two different axes.

I cannot manage to get the axes aligned and to make the second axis invisible... I tried a couple if things (see comments in the code), but they did not work

clearvars
close all 
clc

[X1,Y1,Z1] = peaks(25);

[X2,Y2,Z2] = peaks(25);
idx = find(X2>0);
Z2(idx) = NaN;


figure
set(gcf, 'Position', [0 0 800 800])
%%title
title('')

%%Create two axes
ax1 = axes;
pcolor(X1,Y1, Z1); shading flat

view(2)
ax2 = axes;
pcolor(X2,Y2, Z2); shading flat

%%link them
linkaxes([ax1,ax2]) %<==it didn't work

%ax1.XLim=[-3 3]; %<==I also tried this
%ax2.XLim=[-3 3];
%ax1.YLim=[-3 3];
%ax2.YLim=[-3 3];

%%Hide top axes 
ax2.Visible = 'off'; %<== I thought that this would work
ax2.XTick = [];
ax2.YTick = [];

%%Colormaps
colormap(ax1, bone)
colormap(ax2, jet(26))

%%Add colorbars
set([ax1,ax2],'Position',[.17 .11 .685 .815]);
cb1 = colorbar(ax1,'position',[.08 .11 .03 .815]);
set(get(cb1,'ylabel'),'String','whatever','interpreter','latex', 'fontsize',20);
cb2 = colorbar(ax2,'position',[.92 .11 .03 .815]);
set(get(cb2,'ylabel'),'String','whatever','interpreter','latex', 'fontsize',20);

caxis(ax1,[-7 7])
caxis(ax2,[-5 5])

xlabel(ax1,'stuff')
ylabel(ax1,'other stuff')

fig

Note: I am using 2017a

Upvotes: 3

Views: 198

Answers (1)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23685

I really have no idea whether your approach is correct or not since I never attempted to produce a similar plot before. What I know is that when a figure is created, it already includes a default axis. Thus, calling the function axes() twice inserts two supplementar axes in the figure... and this is the proof:

Axes

Neither ax1 nor ax2 are the cause of this problem. The mismatching axis is the third one, the default one that was created together with the figure instance. What's happening in between is kinda weird (I spent some time trying to debug everything properly, but it's still not clear how instances are being handled)... anyway I found a workaround for deleting it:

clc();
clearvars();
close all;

[X1,Y1,Z1] = peaks(25);

[X2,Y2,Z2] = peaks(25);
idx = find(X2 > 0);
Z2(idx) = NaN;

f = figure();
set(gcf,'Position',[0 0 800 800])
title('');

ax1 = axes();
pcolor(X1,Y1,Z1);
shading flat;
xlabel(ax1,'stuff')
ylabel(ax1,'other stuff')

view(2);
ax2 = axes();
pcolor(X2,Y2,Z2);
shading flat;
ax2.Visible = 'off';

colormap(ax1,bone());
colormap(ax2,jet(26));

set([ax1,ax2],'Position',[.17 .11 .685 .815]);
cb1 = colorbar(ax1,'position',[.08 .11 .03 .815]);
set(get(cb1,'ylabel'),'String','whatever','interpreter','latex', 'fontsize',20);
cb2 = colorbar(ax2,'position',[.92 .11 .03 .815]);
set(get(cb2,'ylabel'),'String','whatever','interpreter','latex', 'fontsize',20);

caxis(ax1,[-7 7]);
caxis(ax2,[-5 5]);

set(ax1,'Tag','keep');
set(ax2,'Tag','keep');

delete(findall(f,'Type','Axes','-not','Tag','keep'));

Since the axes you create are referenced into a variable, once the plotting has been performed you assign the same Tag property to both. Then, using the findall function on the figure handle, you find the third axis (which is the one without the predefined Tag) and you delete it. Result:

Result

EDIT

After further investigations, the followin code can be used instead for producing a cleaner version of this plotting:

clc();
clearvars();
close all;

[X1,Y1,Z1] = peaks(25);

[X2,Y2,Z2] = peaks(25);
idx = find(X2 > 0);
Z2(idx) = NaN;

f = figure();
set(gcf,'Position',[0 0 800 800])

ax1 = axes();
pcolor(X1,Y1,Z1);
shading flat;
xlabel(ax1,'stuff')
ylabel(ax1,'other stuff')

view(2);
ax2 = axes();
pcolor(X2,Y2,Z2);
shading flat;
ax2.Visible = 'off';

colormap(ax1,bone());
colormap(ax2,jet(26));

set([ax1,ax2],'Position',[.17 .11 .685 .815]);
cb1 = colorbar(ax1,'position',[.08 .11 .03 .815]);
set(get(cb1,'ylabel'),'String','whatever','interpreter','latex', 'fontsize',20);
cb2 = colorbar(ax2,'position',[.92 .11 .03 .815]);
set(get(cb2,'ylabel'),'String','whatever','interpreter','latex', 'fontsize',20);

caxis(ax1,[-7 7]);
caxis(ax2,[-5 5]);

Upvotes: 2

Related Questions