Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Legend With FaceAlpha / 50% Opacity ?

945 views
Skip to first unread message

Brian B

unread,
May 13, 2011, 5:33:04 PM5/13/11
to
I found this: http://www.mathworks.com/support/solutions/en/data/1-1U9P1F/index.html?product=ML&solution=1-1U9P1F , however what I really want is for the background of my legend to be white at 50% opacity.

Is this possible?

I wish this would work!
set(lgnd,'FaceAlpha','0.5')

Andrew Stevens

unread,
May 13, 2011, 7:17:20 PM5/13/11
to
"Brian B" wrote in message <iqk82g$131$1...@newscl01ah.mathworks.com>...

Brian,

I couldn't find a straight-forward way to do it, but here is a workaround:

xi=linspace(0,1,20);
yi=xi;

figure
plot(xi,yi)
set(gca,'color','k')

leg=legend('line');
set(leg,'box','off')
ch=findall(leg,'type','axes');

ah=annotation('rectangle',get(ch,'position'));
set(ah,'facecolor','w','facealpha',0.5);

uistack(leg,'top')

-Andrew

Brian B

unread,
May 16, 2011, 2:12:04 AM5/16/11
to
Andrew,

Thank you so much for the help, this works well for a single plot, but for subplots I'm having some issues. Only the last subplot gets the uistack(handle,'top') function call.

How did you know to access the legend in the way you did with findall and annotation? Is there documentation out there on your technique? I found this: http://www.mathworks.com/help/techdoc/creating_plots/braliom.html , however I'm really confused as how it works.

For instance:
-------------------------------------- CODE ----------------------------
hAnnotation = get(legend_handle,'Annotation'),
hLegendEntry = get(hAnnotation','LegendInformation')
-------------------------------------- WORKSPACE ------------------
??? Error using ==> get
There is no 'Annotation' property in the 'legend' class.

Regards,
Brian

Brian B

unread,
May 16, 2011, 2:19:05 AM5/16/11
to
Andrew,

Thank you so much for your help. This works great for a single plot, but for multiple legends in subplots I'm having issues with uistack(handle,'top'); every legend but the last has the legend font below the 50% opaque box. Any suggestions?

Also how did you know to access the legend in the manner you did? Is there any documentation? I've been looking for a way to access legend properties, found this: http://www.mathworks.com/help/techdoc/creating_plots/braliom.html, but it didn't lead me to a way of assigning the background FaceAlpha.

Thanks,
Brian

Andrew Stevens

unread,
May 16, 2011, 8:00:21 AM5/16/11
to
"Brian B" wrote in message <iqqfko$jj3$1...@newscl01ah.mathworks.com>...

Brian,

There is no facealpha property for a legend, so my workaround is basically making the axes if the legend invisible and putting a transparent patch (annotation) underneath the legend text. I used annotation instead of a patch object because it's position property is specified in figure units as is the legend. Anyway, to access the legend properties, just:

lh=legend(...)

The handle looks like an axes handle and has children to all the elements (lines, text, ect), all of which can be modified through the command, set. Not sure about documentation, but hope this helps.

Andrew

Brian B

unread,
May 17, 2011, 12:20:19 PM5/17/11
to
Andrew,

Thank you for your input. If I could get this to work in subplots it would look really good, however for the mean time I'm going to begrudgingly use this function in the file exchange: http://www.mathworks.com/matlabcentral/fileexchange/31092-legendflex-a-more-flexible-legend to position the legend above the subplots.

Thanks again for taking the time to help me out!

-Brian

Paul Veres

unread,
Mar 9, 2017, 4:07:08 AM3/9/17
to
"Brian B" wrote in message <iqu783$787$1...@newscl01ah.mathworks.com>...
This is several years old but thought I'd add a response for people (like myself) who might be led for an answer to this thread by a Google search.

There is a hidden BoxFace property of legends, itself having several properties. The 'ColorData' can be set with a vector of 4 (alpha value in addition to the RGB values). Importantly: the 'ColorType' property must be set to 'truecoloralpha' first so the alpha value is being interpreted; the vector must be manually set to uint8 data type; the vector must be a column vector.

set(*your legend handle*.BoxFace, 'ColorType', 'truecoloralpha', 'ColorData', uint8(255*[1; 1; 1; 0.8]))

The example above will give you 80% transparency of the legend's background. Obviously you need to get a handle when you are creating your legend first.
0 new messages