Why does my plot, created with the PLOTYY function in MATLAB 6.5 (R13) or later, display data in an unexpected way when panning and zooming?

1 view (last 30 days)
I am executing the following code:
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
When I enable ZOOM mode and press the mouse button, the axis zooms in the second plot only. As a result the xticks and yticks become garbled.
When I enable PAN mode, I can pan only one of the plots interactively. Again, the xticks and yticks are garbled.
When I enable the DATACUSRORMODE (MATLAB 7.0 (R14) and later), I can get the data values of the second plot (obtained using the PLOTYY function) only. I want the DATACURSORMODE function to obtain the data values of the first plot as well.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Apr 2023
Edited: MathWorks Support Team on 13 Apr 2023
This enhancement has been incorporated in Release 2008a (R2008a). For previous product releases, read below for any possible workarounds:
By design, the function PLOTYY has two axes superimposed. This behavior occurs because the second axis is frontmost when you use the PLOTYY function.
There is no workaround for the PAN functionality.
As a workaround for the ZOOM functionality, you can download the following file from the MATLAB Central File Exchange:
Note that MathWorks does not control the content posted by visitors to MATLAB Central, and does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will MathWorks be liable in any way for any content not authored by MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted, or otherwise made available via MATLAB Central.
To use the DATACURSORMODE function with the other (first) axis, you have to change the stacking order of the two axes and bring the first axis to the front. Code which does this is given below:
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
set(AX(1),'color','none');
uistack(AX(1),'up'); % To bring the first axis AX(1) in the front.
datacursormode on;
set(AX(2),'color','w');
You can also use this approach to create datatips for graphs of data that you add to an existing plot, by obtaining the handle to the lineseries, barseries or other type of symbology and bringing it to the top with the UISTACK function.

More Answers (0)

Categories

Find more on Two y-axis in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!