How do I make the background of a plot legend transparent?
358 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Jun 2009
Commented: Walter Roberson
on 20 May 2021
The default background color of a plot legend is white. I want to make it transparent.
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
The background of a plot legend can be made transparent by setting the 'color' property of the legend to 'none' . This is shown in the following code sample:
plot(rand(5));
lgnd = legend('one','two','three','four','five');
set(lgnd,'color','none');
3 Comments
jessupj
on 14 May 2020
Edited: jessupj
on 14 May 2020
I think what SN means it is possible the to set the opacity i.e. control the alpha of the background.
E.g. this won't work:
lgnd.Color = [1 1 1 0.75];
The OP's question is answered, but this isn't a general solution.
Instead, you need to do something like:
lgnd.BoxFace.ColorType='truecoloralpha'
lgnd.BoxFace.ColorData=uint8(255*[1 1 1 0.75]')
This works even with Box='off', but you may need to set EdgeColor='none' to remove it again after setting the BoxFace properties. Boxless semi-transparent legend backgrounds are my preference.
Maybe this is documented somewhere, but I can't find it to provide a reference. Note that BoxFace doesn't seem to be a visible property of the legend in the console.
More Answers (0)
See Also
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!