How do I make both yyaxis axes zoom together?
65 views (last 30 days)
Show older comments
MathWorks Support Team
on 15 Feb 2017
When I plot two figures using "yyaxis" and use the interactive zoom, only the right (active) axis zooms. The other one does not get updated. How can I fix this?
Accepted Answer
MathWorks Support Team
on 15 Feb 2017
This behavior is in accordance with the "yyaxis" function documentation (See under "Axes Properties" in the "More About" section of the link below)
However, you can achieve the desired behavior by following these steps:
1. Plot your data using "yyaxis" as normal
2. Create a callback function that, before the zoom happens, saves the old axis limits.
3. Create a callback function that, after the zoom happens, edits the axis values for the (previously unaffected) left axis.
In the attached files, file saveAxisStatus.m defines the pre-zoom callback behavior, and zoomSecondAxis.m defines the post-zoom callback behavior. The file Example.m demonstrates how to use these two functions.
0 Comments
More Answers (4)
hans
on 19 Sep 2018
This works for me (R2018a), as long as I do not choose "Reset to original view" or "Zoom out" after zooming in.
If I try this I get the following warnings:
>> Example Warning: An error occurred during the mode callback. > In matlab.uitools.internal.uimode/fireActionPreCallback (line 16) In zoom>localUIContextMenuCallback (line 2108) Warning: An error occurred during the mode callback. > In matlab.uitools.internal.uimode/fireActionPostCallback (line 16) In zoom>localUIContextMenuCallback (line 2110)
0 Comments
Shawn Treacy
on 29 Mar 2019
Edited: Shawn Treacy
on 1 Apr 2019
I tried to expound upon what Mathworks support did with linking (and unlinking) zoom and pan as well as for switching between the left and right axes and resetting all of the axes. I created buttons and placed them on a plot for testing the code. I'm using R2018a and it seems to be working fine.
The code can be tested by running 'Figure_and_axes_for_linking_zoom_and_pan.m', which calls the function 'link_zoom_and_pan.m'. I haven't tested it in any other versions.
You can get the axes out of whack by zooming in on the figure, then linking zoom, and then zooming out, which is why I added a button for resetting the Y axes.
EDIT: the code works in R2019a. I uncommented lines 7-8 in link_zoom_and_pan.m to get rid of the error that appears when using the linked zoom.
The error says:
Warning: An error occurred during the mode callback.
> In matlab.uitools.internal.uimode/fireActionPostCallback (line 16)
In zoom>local2DButtonUpFcn (line 1383)
In zoom>@(o,e)local2DButtonUpFcn(o,e,hMode,buttonDownData) (line 1134)
In hgfeval (line 62)
In matlab.uitools.internal.uimode/modeWindowButtonUpFcn (line 52)
In matlab.uitools.internal.uimode/setCallbackFcn>localModeWindowButtonUpFcn (line 56)
Warning: An error occurred during the mode callback.
> In matlab.uitools.internal.uimode/fireActionPostCallback (line 16)
In zoom>localApplyZoomFactor (line 849)
In zoom>local2DButtonUpFcn (line 1282)
In zoom>@(o,e)local2DButtonUpFcn(o,e,hMode,buttonDownData) (line 1134)
In hgfeval (line 62)
In matlab.uitools.internal.uimode/modeWindowButtonUpFcn (line 52)
In matlab.uitools.internal.uimode/setCallbackFcn>localModeWindowButtonUpFcn (line 56)
I'm not sure what is causing this, but it doesn't seem to be causing any problems.
0 Comments
Andres
on 6 Dec 2021
I have uploaded a solution (yyzoom) to the File Exchange,
that stores the base values for the axes limits relation inside the callback function of a listener to the YLim property of the yyaxis chart axes. I've had the opportunity to test it with R2021a/b up to now.
0 Comments
See Also
Categories
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!