linkaxes and hold on issues

5 views (last 30 days)
Marco
Marco on 20 Nov 2014
Commented: Marco on 20 Nov 2014
Hi i noticed that when i use linkaxes in combination with hold on i don't get the result i expect.
My objective is to draw polygons at different times in a main figure while keeping a secondary figure axis extent aligned to the main one.
can someone explain why this happens and, more importantly, what should i do to obtain the expected behaviour?
i'll brake the process down in two stages to clarify the unexpected behaviour.
  • Stage 1 - drawing the two figures on the main axis completely disregarding the secondary axis.
  • Stage 2 - adding the secondary into play with linkaxes
Stage 1
ah_main=subplot(1,2,1); %main axis
ah_secondary=subplot(1,2,2); %secondary axis
smallBox.x=[0 1 1 0 0];
smallBox.y=[0 0 1 1 0];
bigBox.x=smallBox.x*3 + 2;
bigBox.y=smallBox.y*3;
hold(ah_main,'on')
plot(ah_main,smallBox.x,smallBox.y,'r', 'linewidth',2)
plot(ah_main,bigBox.x,bigBox.y,'g', 'linewidth',2)
as expected the figure resulting from the execution of this code is:
Stage 2 If i insert a linkaxes before drawing the figures then the behaviour even on the main axis is completely different: the axis limits are not scaled any longer.
ah_main=subplot(1,2,1); %main axis
title('Main axis')
ah_secondary=subplot(1,2,2); %secondary axis
title('Secondary axis')
smallBox.x=[0 1 1 0 0];
smallBox.y=[0 0 1 1 0];
bigBox.x=smallBox.x*3 + 2;
bigBox.y=smallBox.y*3;
linkaxes([ah_main ah_secondary]);
hold(ah_main,'on')
plot(ah_main,smallBox.x,smallBox.y,'r', 'linewidth',2)
plot(ah_main,bigBox.x,bigBox.y,'g', 'linewidth',2)
Any clues?
Thank you very much for your time.

Accepted Answer

Adam
Adam on 20 Nov 2014
Edited: Adam on 20 Nov 2014
linkaxes sets the XLimMode and YLimMode properties to 'manual'
Try inserting
ah_main.XLimMode = 'auto';
ah_main.YLimMode = 'auto';
or
set( ah_main, 'XLimMode', 'auto' )
set( ah_main, 'YLimMode', 'auto' )
if you are working in R2014a or earlier or wish to retain backwards compatibility.
between your linkaxes instruction and the plotting instruction.
By the way, I don't know if this is documented anywhere with regard to linkaxes, I just took a look at axes properties after running your code and noticed the fact. Having noticed it it is not surprising I guess as a side-effect of linkaxes, but not obvious before seeing it! More obvious behaviour would have been for it to keep the main axes as 'auto' and switch any linked axes to 'manual' mode I would have thought.
  1 Comment
Marco
Marco on 20 Nov 2014
that worked great, thanks!
thumbs up for your answer!

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!