Q:

Matlab - highlight a specific interval using patch

I would like to highlight a specific interval in my plot. I found a way using the patch object.
The problem is that my layout gets messy whenever i use it. 
Here's an example of a plot:
x = 1:0.05:25;
plot(x,sin(x)+sin(100*x)/3);
xlabel('x axis');
ylabel('y axis');
title('\Omega \Delta test');
legend('sin \Sigma')

And to highlight a period:
yl = ylim;
patch([5 5 10 10],[yl(1) yl(2) yl(2) yl(1)],'k',...
    'facecolor',[.4 .6 .4],'edgecolor',[.4 .6 .4],...
    'facealpha',0.3,'edgealpha',0.3)

My results with and without the patch command:
Normal:

Messy:

Look at the fonts and the legend block.  Any ideas on how to fix that?
Is there a better way to highlight an interval? I need to choose the color and set transparency.
Just one more question: Why do I have to use the third input (color) if it's not applied?
Thanks in advance!

A:

Edit: This answer is only valid for Matlab versions before 2014b, as the incredibly useful erasemode property has been removed from all HG2 graphic objects on later Matlab versions.

I ran into this problem countless times and I had to learn to live with it. Most times I can accept the glitches of the OpenGL renderer if it buys me nice transparency effects, but in some cases it is just not acceptable.
I use patch objects to highlight intervals in many applications, usually over several curves. There is a trick you can use when transparency is not an option, it is the EraseMode property of the patch object. If you set the EraseMode property to 'xor' the patch will not be transparent, yet anything under the patch will xor the patch pixel colours so you can still see the curves under the patch.
This not being a transparency rendering, you can use the default painter renderer and avoid all the occasional glitches of the OpenGL.
So for example with your data:
hp = patch([5 5 10 10],[yl(1) yl(2) yl(2) yl(1)],'k',...
    'facecolor','g','edgecolor','g',...
    'erasemode','xor') ;

And the nice advantage of this trick is it works with monochrome display/prints. If you cannot use multiple colours, you can use that with only one colour (if you plan black & white printing for publication for example)
hpx = patch([5 5 10 10],[yl(1) yl(2) yl(2) yl(1)],'b',...
    'facecolor','b','edgecolor','b',...
    'erasemode','xor') ;

