File Exchange

image thumbnail


version (56.5 KB) by Chad Greene
legend with support for patch transparency.


Updated 15 Aug 2014

View License

If you've plotted semitransparent patch objects and tried to include them in a legend, you may have noticed that legend does not include transparency. This function solves the problem simply. Use legalpha just as you'd use legend, and if legalpha finds patch objects with transparency associated with them, the patches will be displayed properly in the legend.

Cite As

Chad Greene (2021). legalpha (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (7)

Alex Brisbourne

Matthew Cooper

On version 2018a I receive an error if there is only one patch object in the figure.

Line 34 returns class double if there is only one patch object.

Line 44 then uses brace indexing which fails if the return to line 34 is class double.

Fix by adding the following after Line 34:
hlaFA = num2cell(hlaFA);

Kai-Jimmy Shen

As Matthew stated, line 40 needs to be updated from
hlp = findobj(legend_h,'type','patch');
hlp = findobj(object_h,'type','patch');

Using 2016b, I also had to add
hlaFA = flipud(hlaFA)
after line 34.

Thanks for sharing!

Frederik Rösch


Many thanks for your function. Since we run a centralized function in order to handle diagram properties that handles a lot of different types of figures I had crashes in some cases. To solve this I had to add an ~isempty query in case the first line

hla = findobj(gca,'type','patch');

cannot find any objects.

I added the ~isempty query to the "Set alpha values" code:

if ~isempty(hla)
for k = 1:length(hlp)

Matthew Cooper

Need to update line 40 from

hlp = findobj(legend_h,'type','patch');


hlp = findobj(object_h,'type','patch');

for compatibility with new legend handle behavior (as noted by Chad Greene)

Chad Greene

Thanks for the feedback, Eric. I will try to update this function for R2014b soon.


Very helpful code! Though I think I found a bug or error in the code here (or possibly just an ambiguity). If I am trying to label objects in my plot created by the "fill" command, the "hlp = findobj(legend_h,'type','patch');" returns an empty array (for whatever reason) and doesn't give the correct alpha values to the legend. However, if I search "object_h" for "patch" objects, it will give me the correct handles. I am using 2014b, if that makes any difference.

Thank you!

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

Inspired by: legappend

Inspired: histf

Community Treasure Hunt

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

Start Hunting!