MATLAB: How to draw transparent lines in MATLAB R2011a (7.12)

alphalineMATLABpatchplottransparencytransparent

I want to plot transparent lines (as opposed to surfaces or patches).

Best Answer

Currently, MATLAB line objects don't have a transparency property and thus can't be made transparent. In other words, commands such as PLOT can't create transparent lines.
A workaround involves using the PATCH function to draw a patch that looks like a line and setting the "EdgeAlpha" property.
x = [0 10 20 50 60];
y = [0 5 10 5 0];
xflip = [x(1 : end - 1) fliplr(x)];
yflip = [y(1 : end - 1) fliplr(y)];
patch(xflip, yflip, 'r', 'EdgeAlpha', 0.1, 'FaceColor', 'none');
The "EdgeAlpha" property value determines the transparency of the line. Make sure to set "FaceColor" to "none" as the PATCH function may produce unexpected behavior when collapsed into a line (fill the entire axes with the fill color).
Note that transparency is possible due to the use of the OpenGL renderer. Changing the renderer, or doing any operation that results in the same, will remove the transparency. For example, setting the axis from linear to log will change the renderer to ZBuffer. To re-set the transparency features change the renderer back to OpenGL with the following command:
set(gcf,'renderer','opengl');