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

19 Ansichten (letzte 30 Tage)
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:
figure;
[h, L1, L2] = plotyy(3, 3, 4, 7)
set(h(1),'ytick',[2:0.2:4])
Is there any way to resolve this issue?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 14 Jan. 2013
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
pause;
%%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
pause;
%%link the two X axes
linkaxes(AX,'x');
%%turn off the axis Box property for box axis
% the box property results in the display of tick marks on both axis
set(AX(1),'Box','off')
set(AX(2),'Box','off')
% 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
print(fg,'-dpng','myFigure');

Weitere Antworten (0)

Kategorien

Mehr zu Two y-axis finden Sie in Help Center und File Exchange

Produkte


Version

R2011a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by