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

I saw this question asked earlier, but there seems to be no solution, so it's just interesting if this is possible at all.

I have a stroke chart in MatLab and 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 reflected in the legend:

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

However, the alpha value in the legend seems to be 1.

Any ideas? Thank.

+4
source share
3 answers

You can use PatchInLegend = findobj(BL, 'type', 'patch');to find patch objects in your legend. You can then set their transparency using set(PatchInLegend, 'facea', 0.5)to set their transparency.

To transparent

enter image description here

After transparent

enter image description here

So the color is changing and it looks much better.

+5

, 2014b . .. "", , ;

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

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

. , , , :)

+10

, , , , ( ), BoxFace , , .

lgnd = legend(legendTextCells);
set(lgnd.BoxFace, 'ColorType', 'truecoloralpha', 'ColorData', uint8([255;255;255;0.5*255]));

legendTextCells - , , 0.5*255.

+2

Source: https://habr.com/ru/post/1540827/


All Articles