File Exchange

image thumbnail


version (2.75 KB) by Brett Shoelson
Visualize lines as patch objects. Specify any valid patch property, including transparency.


Updated 01 Sep 2016

View License

Using very simple syntax, patchline allows you to visualize lines as patches. This allows you to set any valid properties of the patch object, including 'EdgeAlpha' (to modify the transparency of the lines).
Note that the effectiveness of setting the transparency (edgealpha) may differ on different operating systems. Try different Figure Renderers (particularly openGL) to get the desired behavior.

Also, unix users may experience problems if the renderer is not set to "opengl software". This parameter cannot be set at runtime, but can be set in startup.m.

Comments and Ratings (10)

Very nice work!

Brett Shoelson

Hi Kouichi,
Thanks for sharing this. Yes, Yair's tips can be quite useful. The only caveat: you incur a risk when using undocmented MATLAB, since functionality is apt to break in a future release.
So yes, use the 4-element specification of line color. But keep the documented approach in patchline in your back pocket just in case. ;)


Hi Brett,

You might remember that I've uploaded linealpha() which is essentially the same as patchline(). The other day I found even better solution. Simply adding the fourth element for alpha to 'Color' property of Primitive Line objects, you can control transparency of lines (R2014b or later). Just thought you should also know about this.

plot([1,10],'Color',[1 0 0 0.2])

Sorry I'm so late to discover this, but this is Brilliant. I've wanted to do this for years and only now just searched to see if someone had already done it. Just Brilliant!

Hyongju Park



K E (view profile)

When you need a semi-transparent line, this will do it.

Roger Parkyn

This is pretty useful.

To enable it to handle a matrix of y-values (column-wise) I made the following change which you may like to incorporate:

ADDED AFTER LINE 97 ("% Facecolor = 'k' is (essentially) ignored"...)
%Make x-vector the same size as y-matrix (assuming data is in columns)
if size(ys,2)>1 && size(xs,2)==1
xs = repmat(xs,1,size(ys,2));
%Add NaNs onto bottom row (so it doesn't plot an area?)
xs = [xs; NaN(1, size(xs,2))];
ys = [ys; NaN(1, size(ys,2))];
% Plot the "patches" (actually lines)
if isempty(zs)
p = patch(xs, ys, 'w');
p = patch(xs, ys, zs, 'w');

% if isempty(zs)
% p = patch([xs(:);NaN],[ys(:);NaN],'k');
% else
% p = patch([xs(:);NaN],[ys(:);NaN],[zs(:);NaN],'k');
% end



Updated license

Improved the rice.png example, modified the description slightly.

Updated description, examples.

MATLAB Release Compatibility
Created with R2012a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: linealpha(x,y,varargin), customGrid, Plot Food Web, FadeIT

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor