How can I obtain opaque patch in a subplot with a mesh command in another subplot?

1 view (last 30 days)
The command
patch_1 = patch(x_circle,y_circle,'r');
set(patch_1,'facealpha',1);
does not make the circles opaque in a subplot if in another subplot there is the command mesh.
Example:
x=-1:0.1:1;
y=-1:0.1:1;
[x,y]=meshgrid(x,y);
x=x';
y=y';
f=inline('x.^2+y.^2','x','y');
f=feval(f,x,y)
clf
subplot(1,2,2)
theta=0:0.1:2*pi;
x_circle=-0.5+cos(theta);
y_circle=0+sin(theta);
patch_1 = patch(x_circle,y_circle,'r');
set(patch_1,'facealpha',1);
x_circle=cos(theta);
y_circle=sin(theta);
patch_2 = patch(x_circle,y_circle,'r');
set(patch_1,'facealpha',1);
x_circle=0.5+cos(theta);
y_circle=0+sin(theta);
patch_3 = patch(x_circle,y_circle,'g');
set(patch_3,'facealpha',1);
axis equal
axis([-2 2 -2 2])
subplot(1,2,1)
mesh(x,y,f)
axis([-1 1 -1 1 0 2])
without the subplot(1,2,1) we obtain three opaque circles, but with the two subplot in the same figure windows, the contour lines of the three circles appear overlapped and transparent.
Can I obtain (Matlab2011b) the circles completely opaque (also for the contour lines) in the subplot?
Thank you

Accepted Answer

Mike Garrity
Mike Garrity on 19 May 2015
You're seeing the edges of the red circles poke through the green circle, aren't you?
This isn't actually caused by alpha. Here's what's going on. When you use OpenGL's depthsort, things can get a bit squirrely with coplanar objects. To avoid these problems, we add a tiny bit of Z offset between the face and the edge. This ensures that you always see the edges instead of having them come and go.
In 11b, when you added the mesh, it switched the figure's Renderer property to 'opengl'. That caused this polygon offset issue to show up in your circles. You can set the renderer back to painters like this:
set(gcf,'Renderer','painters')
If your mesh was really complex, then you might encounter some performance problems or bugs using painters. That's why it switched to opengl.
If you upgrade to R2014b or R2015a, then you have some more options. The biggest difference is that there's a new SortMethod property on the axes. I talked about that in some detail in this post. This allows you to use the opengl renderer without getting the depth sorting. This will give you the best of both worlds in this case. In fact, the system recognizes that your axes with the circles doesn't need depthsort and automatically chooses SortMethod='childorder' for it. This means that if you run this code in one of these newer versions, you'll get this:
I think that's what you're after, isn't it?

More Answers (0)

Community Treasure Hunt

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

Start Hunting!