调整图形大小时辅助轴移位
Secondary axes get displaced when resizing figure
我正在尝试在图形的底部和顶部绘制具有两个 X 轴的 Matlab (R2017a) 图(填充轮廓 + 颜色条),比例相同但刻度线和标签不同。按照发现的建议 here and here,我已经实现了,但是当我尝试手动调整图形 window 的大小时或打印它时设置与默认比例不同的某些比例,例如:
set(gcf,'PaperUnits','centimeters','PaperPosition',[0 0 30 15])
print(gcf,'-dpng',path,'-r300')
新轴移位:
我已经用来自 Matlab 的 peaks 示例数据重现了我的问题:
contourf(peaks)
ax1=gca;
colorbar
set(ax1,'box','off','color','none') % get rid of the box in order not to have duplicated tick marks
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,... % set the new pair of axes
'XAxisLocation','top',...
'YAxisLocation','Right',...
'Color','none');
set(ax2, 'XLim', get(ax1, 'XLim'), 'YLim', get(ax1, 'YLim')); % set same limits as for ax1
set(ax2, 'XTick', 0:14:42, 'XTickLabels', {'a','a','a','a'},... % set new tick marks and labels for the top X axis.
'YTick', get(ax1, 'YTick'), 'YTickLabels', []);
奇怪的是,如果我删除 colobar 命令并只绘制填充的轮廓,图形就会正确运行:
有谁知道为什么会这样(以及如何解决)?我也愿意通过其他方式实现双 X 轴图。
也尝试将 'PaperPositionMode'
设置为 'auto'
:
set(gcf,'PaperUnits','centimeters','PaperPosition', [0 0 30 15], 'PaperPositionMode', 'auto');
% then print
print(gcf, '-dpng', 'myFile', '-r300')
以上命令对我有用。产生以下结果:
您的问题是一个轴有颜色条而另一个没有,即使您将颜色条添加到两个轴,也可能会发生很多自动操作,这些操作会以不同方式调整轴的大小。
但是,我们可以添加一个事件侦听器并定义一个函数来操作使两个轴相同。侦听器将确保它捕获事件(调整大小)并调用我们定义的函数。这是我为此编写的代码:
%% this creates the listener for change of size in the figure
f = figure('SizeChangedFcn',@(src,evn) fixaxis(src));
%% this is your code
contourf(peaks)
ax1=gca;
colorbar
set(ax1,'box','off','color','none') % get rid of the box in order not to have duplicated tick marks
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,... % set the new pair of axes
'XAxisLocation','top',...
'YAxisLocation','Right',...
'Color','none');
set(ax2, 'XLim', get(ax1, 'XLim'), 'YLim', get(ax1, 'YLim')); % set same limits as for ax1
set(ax2, 'XTick', 0:14:42, 'XTickLabels', {'a','a','a','a'},... % set new tick marks and labels for the top X axis.
'YTick', get(ax1, 'YTick'), 'YTickLabels', []);
%% this will resize the axis if 2 of them exist
function fixaxis(src)
ax=findall(src,'Type','Axes');
if length(ax)==2
ax(2).Position=ax(1).Position;
end
end
我正在尝试在图形的底部和顶部绘制具有两个 X 轴的 Matlab (R2017a) 图(填充轮廓 + 颜色条),比例相同但刻度线和标签不同。按照发现的建议 here and here,我已经实现了,但是当我尝试手动调整图形 window 的大小时或打印它时设置与默认比例不同的某些比例,例如:
set(gcf,'PaperUnits','centimeters','PaperPosition',[0 0 30 15])
print(gcf,'-dpng',path,'-r300')
新轴移位:
我已经用来自 Matlab 的 peaks 示例数据重现了我的问题:
contourf(peaks)
ax1=gca;
colorbar
set(ax1,'box','off','color','none') % get rid of the box in order not to have duplicated tick marks
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,... % set the new pair of axes
'XAxisLocation','top',...
'YAxisLocation','Right',...
'Color','none');
set(ax2, 'XLim', get(ax1, 'XLim'), 'YLim', get(ax1, 'YLim')); % set same limits as for ax1
set(ax2, 'XTick', 0:14:42, 'XTickLabels', {'a','a','a','a'},... % set new tick marks and labels for the top X axis.
'YTick', get(ax1, 'YTick'), 'YTickLabels', []);
奇怪的是,如果我删除 colobar 命令并只绘制填充的轮廓,图形就会正确运行:
有谁知道为什么会这样(以及如何解决)?我也愿意通过其他方式实现双 X 轴图。
也尝试将 'PaperPositionMode'
设置为 'auto'
:
set(gcf,'PaperUnits','centimeters','PaperPosition', [0 0 30 15], 'PaperPositionMode', 'auto');
% then print
print(gcf, '-dpng', 'myFile', '-r300')
以上命令对我有用。产生以下结果:
您的问题是一个轴有颜色条而另一个没有,即使您将颜色条添加到两个轴,也可能会发生很多自动操作,这些操作会以不同方式调整轴的大小。
但是,我们可以添加一个事件侦听器并定义一个函数来操作使两个轴相同。侦听器将确保它捕获事件(调整大小)并调用我们定义的函数。这是我为此编写的代码:
%% this creates the listener for change of size in the figure
f = figure('SizeChangedFcn',@(src,evn) fixaxis(src));
%% this is your code
contourf(peaks)
ax1=gca;
colorbar
set(ax1,'box','off','color','none') % get rid of the box in order not to have duplicated tick marks
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,... % set the new pair of axes
'XAxisLocation','top',...
'YAxisLocation','Right',...
'Color','none');
set(ax2, 'XLim', get(ax1, 'XLim'), 'YLim', get(ax1, 'YLim')); % set same limits as for ax1
set(ax2, 'XTick', 0:14:42, 'XTickLabels', {'a','a','a','a'},... % set new tick marks and labels for the top X axis.
'YTick', get(ax1, 'YTick'), 'YTickLabels', []);
%% this will resize the axis if 2 of them exist
function fixaxis(src)
ax=findall(src,'Type','Axes');
if length(ax)==2
ax(2).Position=ax(1).Position;
end
end