3

我有一个带有两个不同轴的图形。

我无法使轴对齐并使第二个轴不可见...我尝试了一些 if 事情(请参阅代码中的注释),但它们不起作用

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')

图

注意:我使用的是 2017a

4

1 回答 1

2

我真的不知道你的方法是否正确,因为我以前从未尝试过制作类似的情节。我所知道的是,当创建一个图形时,它已经包含一个默认轴。因此,两次调用该函数axes()会在图中插入两个补充轴......这就是证明:

轴

既不ax1是也不ax2是这个问题的原因。不匹配的轴是第三个轴,这是与图形实例一起创建的默认轴。两者之间发生的事情有点奇怪(我花了一些时间尝试正确调试所有内容,但仍不清楚如何处理实例)......无论如何我找到了删除它的解决方法:

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'));

由于您创建的轴被引用到变量中,因此一旦执行了绘图,您就可以为Tag两者分配相同的属性。然后,使用图形句柄上的findall 函数,找到第三个轴(即没有预定义的轴Tag)并将其删除。结果:

结果

编辑

经过进一步调查,可以使用以下代码来生成此绘图的更清晰版本:

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]);
于 2018-02-21T21:03:26.230 回答