plotyy一直是我最喜欢的喜欢讨厌的MATLAB函数之一.这是一个非常有用的功能,我似乎总是遇到错误,我已经完全停止使用它,只支持堆叠两个(或更多)轴对象并分别绘制它们.然后,您可以将“子”轴的“位置”属性设置为与主轴相同,它们将很好地堆叠.
一个功能的例子:
xx = linspace(-15,15,100);
yy1 = sin(xx);
yy2 = cos(xx);
f = figure('Color','white');
ax(1) = axes('Parent', f);
ax(2) = axes('Parent', f, 'Color', 'none', 'XTick', [], 'YAxisLocation', 'right');
p1 = plot(ax(1), xx, yy1);
hold(ax(2), 'on'); % Hold to preserve our axes properties set above
p2 = plot(ax(2), xx, yy2);
hold(ax(2), 'off');
ylabel(ax(1),'Phase','FontSize',18);
ylabel(ax(2),'Spectrum','FontSize',18);
set(ax,{'ycolor'},{'k';'k'});
set(p1,'LineWidth',2,'Color',[0.4940,0.1840,0.5560]);
set(p2,'LineWidth',2,'Color','red');
xlabel(ax(1),'Frequency [THz]','FontSize',18);
set(ax,'FontSize',14)
set(ax, 'ActivePositionProperty', 'position'); % Resize based on position rather than outerposition
set(ax(2), 'Position', get(ax(1), 'Position')); % Set last to account for any annotation changes
除了堆叠轴,您还会注意到我已将ActivePositionProperty设置为位置(而不是外部位置). MATLAB resizes axes automatically当Units属性设置为Normalized时,似乎这是问题产生的主要位置.调整大小时,MATLAB还会修改第二个轴的OuterPosition值,使其调整绘图部分的大小.在我的情况下,差异很小,[0 0 1 1]与[0 0.0371 1.0000 0.9599],但效果显然非常明显.您可以使用get和set来解决此问题,但是您必须在每次调整时都这样做,这非常烦人.另一种方法是根据位置调整大小,这似乎可以缓解这个问题,并且是R2015b实现plotyy的一个调整.除了窗口非常小的情况之外,这也修复了plotyy,所以我用更通用的方法留下了答案.