在一个图中对齐轴
align axes in one figure
我有一个图形有两个不同的轴。
我无法使轴对齐并使第二个轴不可见...我尝试了一些方法(请参阅代码中的注释),但它们不起作用
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
我真的不知道你的方法是否正确,因为我以前从未尝试过制作类似的情节。我所知道的是,当创建一个图形时,它已经包含一个默认轴。因此,两次调用函数 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 function,找到第三个轴(没有预定义 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]);
我有一个图形有两个不同的轴。
我无法使轴对齐并使第二个轴不可见...我尝试了一些方法(请参阅代码中的注释),但它们不起作用
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
我真的不知道你的方法是否正确,因为我以前从未尝试过制作类似的情节。我所知道的是,当创建一个图形时,它已经包含一个默认轴。因此,两次调用函数 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 function,找到第三个轴(没有预定义 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]);