MATLAB: Plot opaque element above transparent object

opaquetransparency

Hi, I am plotting inversion results in a pcolor-plot, where I specify the transparency of each data point in dependance on the certainty of the model parameter. NaN is assigned to some model parameters, which appear white. Due to the resolution of the inversion grid, the boundary between the areas for which I have values and the NaN area appears very blocky.
In a plot that uses no transpareny, I can easily plot a white polygon with the "fill"-command which results in a sharp boundary. However, when I plot the white polygon with FaceALpha=1 above transparent elements, I can still see those elements below the polygon.
Here is an example that shows the effect:
clear all
close all
figure
polygon1=fill([1 3 3 1],[1 1 3 3],'r','FaceALpha',0.5);
hold on
polygon2=fill([2 4 4 2],[2 2 4 4],'b','FaceALpha',1);
polygon3=fill([2 4 4 2],[1 1 2 2],'w','FaceALpha',1);
xlim([0 5])
ylim([0 5])
The blue rectangle should cover the red one, why does the overlay appear purple then?

Best Answer

The code draws the polygons in the same z==0 plane. The transparency activates the OpenGL renderer. The renderer has to decide the order of patchs along the view direction, but this order is not well defined in OpenGL, because the renderer does not have any information about the order the objects have been defined inside Matlab - in opposite to the Painters renderer.
So try this:
figure
polygon1=fill3([1 3 3 1],[1 1 3 3],[1,1,1,1], 'r','FaceALpha',0.5);
hold on
polygon2=fill3([2 4 4 2],[2 2 4 4],[2,2,2,2], 'b','FaceALpha',1);
polygon3=fill3([2 4 4 2],[1 1 2 2],[3,3,3,3], 'w','FaceALpha',1);
xlim([0 5])
ylim([0 5])
pause(1)
view(2)