MATLAB Answers


Why do Y-axes tick marks appear on both axes when using PLOTYY in MATLAB 7.12 (R2011a)?

When using PLOTYY in MATLAB 7.12 (R2011a) and previous versions, tick marks appear on both axes when I make modifications to one of the axes. For example, manually setting the tick spacing on the first Y axis, causes two sets of tick labels to appear of the second axes:
[h, L1, L2] = plotyy(3, 3, 4, 7)
Is there any way to resolve this issue?

2 Answers

Answer by MathWorks Support Team on 14 Jan 2013
 Accepted Answer

The function PLOTYY creates two sets of axes and uses the property YAxisLocation to put them on different sides on a figure. By default, Y tick marks are spaced such that the overlap exactly and it appears that there is only one set of tick marks on each of the Y axes.
However, when tick mark properties (such as their quantity, spacing or Y limits) are modified manually, without ensuring that the same number of tick marks is present on both axes, the above behavior is seen (i.e. two sets of tick marks on one of the axes).
The axis property 'Box' controls the presence of Tick marks on both sides on an axis. By default, PLOTYY sets the value of this property to 'off' for the right axes and to 'on' for the left axes (that is why the double tick marks appear on the right axes).Setting the value of the property 'box' to 'off' for both axes will remove the extra sets of tick marks from the other side as well.
However, this will also remove the top edge of the axes (the bounding box) and the image may look borderless when it is saved or printed. As a workaround, one of the two sets of X axes (which were created with PLOTYY) may be moved to the top of the figure, effectively replacing the bounding box along that edge. The two X axes may need to be linked via the command LINKAXES to make sure that the two plots are synchronized when zooming in.
The following code illustrates the issue and the workaround, step by step (note the commands PAUSE in this code):
%%create some data to plot
t = 1:100;
y1 = abs(1e4*sin(.01*t));
y2 = abs(cos(.02*t));
%%create a figure and use PLOTYY, saving the output handles
fg = figure(5);
% note that by default the tick marks overlap exactly (and it seems that
% only one set of tick marks is present per line axis
[AX,H1,H2] = plotyy(t, y1, t, y2);
% press ENTER to continue
%%change the number of tick marks on each Y axis to see the issue
Nticks = 8;
y1 = linspace(0, 8e4, Nticks);
y2 = linspace(0, 14, Nticks+4); %put 4 more ticks on the right Y axis
% set the new limits and tick marks. Notice how there appear to be two sets
% of tick marks on the right axis.
set(AX(1), 'ylim', [y1(1), y1(end)], 'ytick', y1);
set(AX(2), 'ylim', [y2(1), y2(end)], 'ytick', y2);
% press ENTER to continue
%%link the two X axes
%%turn off the axis Box property for box axis
% the box property results in the display of tick marks on both axis
% notice how the bounding box on top of the image is missing
%%replace the top edge of the box with the second X axes
% and turn off its labels
set(AX(2), 'XTickLabel','','XAxisLocation','Top')
%%print the figure to .png format
% to confirm that the output is as desired


Show 1 older comment
Agreed, the solution is fairly useless.
The solution mentioned here is only applicable for the older graphics system prior to MATLAB R2014b. The graphics infrastructure was completely overhauled in R2014b. I suggest you to contact Mathworks Technical Support if you wish to achieve this workflow on a release later than R2014a.
Hi y'all, I just wanted to point out that I took a peak into plotyy() (R2015b, in Feb. 2018), and I saw that for the second axes the Visible property of XRuler was being set to off. Adding:
AX(2).XRuler.Visible = 'on';
before the line setting the x-axis location for the second axis (I'm just reporting what I did, I don't think it matters if you put it before or after the last line) produces the desired behavior.
This is helpful for anyone running R2015b, and who has to use plotyy().

Sign in to comment.

Answer by D
on 25 Dec 2016

Try this
[AX,L1,L2] = plotyy(x1,y1,x2,y2);
refline(AX(1),0,AX(1).YLim(2)) % fills the top part of box
You can later change the color of newly added line. 'refline' will need statistical and machine learning toolbox. Of course you can do this without 'refline' too.


Sign in to comment.