MATLAB: Display rectangle on top of plotyy

drawing orderMATLABplotyyr2015bgraphicsrectangle

Hello community,
I would like to draw a rectangle on top of a plotyy. But the second plot data are always on top of the rectangle. I tried it with uistack, but that isn't working. Here is a small example…
[axh,hLine1,hLine2] = plotyy(1:100,randn(100,1),1:100,randn(100,1));
h = rectangle('Position',[1,0,20,3],'LineWidth',2);
% rectangle on top
uistack(h,'top')
uistack(hLine2,'down')
I really appreciate any help you can provide.
Greetings Sebastian

Best Answer

The first return value from plotyy (axh in your case) is an array of two handles. Try putting the rectangle in the other one. In other words, this:
[axh,hLine1,hLine2] = plotyy(1:100,randn(100,1),1:100,randn(100,1));
rectangle(axh(1),'Position',[1,0,20,3],'LineWidth',2)
is different from this:
[axh,hLine1,hLine2] = plotyy(1:100,randn(100,1),1:100,randn(100,1));
rectangle(axh(2),'Position',[1,0,20,3],'LineWidth',2)