How do I make both yyaxis axes zoom together?

23 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2017 年 2 月 15 日
回答済み: Julian Hapke 2023 年 8 月 11 日
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?

採用された回答

MathWorks Support Team
MathWorks Support Team 2023 年 6 月 1 日
編集済み: MathWorks Support Team 2023 年 6 月 5 日
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.
 

その他の回答 (2 件)

Andres
Andres 2021 年 12 月 6 日
編集済み: MathWorks Support Team 2022 年 6 月 10 日
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.

Julian Hapke
Julian Hapke 2023 年 8 月 11 日
Here's a function that does not rely on an activated zoom tool and also works for panning
function yysync(ax)
%YYSYNC links left and right yyaxis, so they pan and zoom synchronously
%
% unfortunately the PostSet event is also triggered when ylim() is called
% so if you want to set axis limits independently, you have to to so before
% calling yysync
%
% Syntax:
% yysync(ax)
%
% Inputs:
% ax:
% the axes whose yyaxis shall be linked
% graphics handle
% required
%
% Outputs:
% -
%
% Examples:
% -
%
% See also: yyaxis, ylim
%
rulers = ax.YAxis;
if numel(rulers) == 1
% no second y axis
return
end
factor = diff(rulers(2).Limits) / diff(rulers(1).Limits);
offset = rulers(2).Limits(1) - factor * rulers(1).Limits(1);
function setOtherLimits(~, event)
obj = event.AffectedObject;
if obj.YAxisLocation == "right"
obj.YAxis(1).Limits = (obj.YLim - offset) / factor;
else
obj.YAxis(2).Limits = factor .* obj.YLim + offset;
end
end
addlistener(ax, 'YLim', 'PostSet', @setOtherLimits);
end

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by