plotyy is one of my favorite MATLAB features that people love to hate. This is a really useful function that I always encounter errors, right up until the moment when I completely stopped using it, instead of just folding two (or more) axis objects and building them separately. Then you can set the Position property of the axes βunderβ in the same way as your main axes, and they will fold well.
Functional example:
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
Along with stacking the axes, you'll also notice that I set ActivePositionProperty to Position (and not outerposition ). MATLAB automatically resizes axes if the Units property is set to Normalized , and it looks like this is the main place where the problem occurs. When resizing MATLAB also changes the outerposition value for the second axes, causing it to resize the plot area. In my case, the difference is small, [0 0 1 1] vs. [0 0.0371 1.0000 0.9599] , but the effect is clearly very pronounced. You can use get and set to fix this, but you will have to do it every time you resize, which is pretty annoying. An alternative is position-based resizing, which seems to alleviate the problem and is the tweak present in the R2015b plotyy implementation. It also captures plotyy , except when the window is very small, so I left my answer with a more general approach.