MATLAB: Does changing the X-axis in a plot generated with the PLOTYY function corrupt the X-axis in MATLAB

axiscorruptedmangledMATLABoverlapplotyy

Why does changing the X-axis in a plot generated with the PLOTYY function corrupt the X-axis in MATLAB?
I generated a plot using the PLOTYY function. When I try to change the X-axis using the AXIS function or the XLIM function, it becomes corrupted. It appears to have two different sets of numbers and tick marks.

Best Answer

The X-axis is not actually corrupted. The PLOTYY function generates two different axes, one on top of the other. The AXIS and XLIM functions will only modify the axis that was most recently used, so the unmodified axis appears to corrupt the X-axis labels. The object handles will have to be used to modify both axes at once.
For example,
ax = plotyy([0 1 2],[1 0 1],[0 1 2], [5 10 15])
xlim(ax(1),[-1 3])
% the xaxis will be corrupted at this step
xlim(ax(2),[-1 3])
Related Question