Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the alpha value (transparency) of legend to match alpha in plot in MatLab

Tags:

matlab

I've seen this question asked before but there appears to be no solution so am just wondering if it is at all possible.

I have a bar plot in MatLab and have set the transparency:

B = bar(x,y,'stacked');
set(B(1),'facecolor',[0 0.3906 0]) 
set(B(2),'facecolor',[0.5625 0.9297 0.5625])
ch1 = get(B(1),'child');
set(ch1,'facea',.5)
ch2 = get(B(2),'child');
set(ch2,'facea',.5)

And I would like the transparency in the plot to be reflected in the legend:

BL = legend ((B([1 2])),{'data1','data2'},'fontsize',10);

However, it appears the alpha value in the legend is 1.

Any ideas? Thanks.

like image 263
user2861089 Avatar asked May 16 '14 05:05

user2861089


Video Answer


1 Answers

Note with the 2014b update a slight change is needed. The information about patches etc. seems now to be held in the "icons" output from the legend command, so you need;

[BL,BLicons] = legend ((B([1 2])),{'data1','data2'},'fontsize',10);

and then

PatchInLegend = findobj(BLicons, 'type', 'patch');
set(PatchInLegend, 'facea', 0.5)

for this to work now. Just spent an hour figuring this out, so thought I'd pass it on:)

like image 146
James Avatar answered Oct 04 '22 03:10

James