MATLAB: How to use a mouse to move the legend associated with the left axes created by PLOTYY

legendMATLABmoveplotyy

When I associate a legend with each axis created using PLOTYY, I can only move, using the mouse, the legend associated with the right axes. I cannot use the mouse to move the legend associated with the left axes, even if I make the left axes the CurrentAxes.

Best Answer

In order to move the legend associated with the left axis, you need to bring it to the front. Using the LEGEND command with an output variable provides a "handle" to the legend that can be used to bring the legend to the "front" of the figure. This is illustrated by the following example:
x=0:.01:2*pi;
[ax,h1,h2]=plotyy(x,sin(x),x,cos(x)+5);
%leg1 and leg2 are handles for the axes containing legend
%Parent handle for leg1 and leg2 handles is figure handle
leg1=legend(ax(1),'sin');
leg2=legend(ax(2),'cos');
%Activate the legend for left axes ax(1)
axes(leg1)
%Now Mouse can be used to change the position of the legend
%for left axes and right axes as well.