legendflex-pkg
legendflex-pkg copied to clipboard
FaceAlpha is not reflected in the legend
legendflex does not seem to take the FaceAlpha settings of particular bar graph elements into account. With the code, below I find that the legend has only fully opaque bars.
Hb = bar(rand(2));
Hb(1).FaceAlpha=0.1;
legendflex(Hb,{'1','2'}, 'anchor', {'ne','ne'});
This seems to be a bug (or feature?) within legend, which appears when legend is called with multiple outputs. The legendflex function then inherits this behavior. Compare:
Hb = bar(rand(2));
Hb(1).FaceAlpha=0.1;
leg = legend('one', 'two');
to
Hb = bar(rand(2));
Hb(1).FaceAlpha=0.1;
[leg, thing2] = legend('one', 'two');
Unfortunately, I can't think of robust way to account for this in legendflex, since there isn't clear documentation on this behavior of legend, and I'm not sure how many other object properties might be likewise different based on output number. It's not ideal, but manual modification of any legend objects with out-of-sync properties would be my recommended workaround.
Hb = bar(rand(2));
Hb(1).FaceAlpha=0.1;
[leg, thing2] = legendflex({'one', 'two'});
thing2(3).Children.FaceAlpha = 0.1;
The auto-syncing function in the example below seems to work, though admittedly I haven't tested it on a wider range of and mix of plot objects.
Hb= bar(rand(2,3));
Hb(1).FaceAlpha=0.1;
Hb(2).EdgeColor='b';
Hb(3).LineWidth=2;
Hb(3).FaceAlpha=0.3;
[hLegend,objLegend,objPlots]=legendflex(Hb,{'1','2','3'},'buffer',[60,-5]);
syncprops({'FaceAlpha','FaceColor','EdgeAlpha','EdgeColor'},objLegend,objPlots)
function syncprops(Props,objLegend,objPlots)
objleg=findobj(objLegend,'-not','type','text','-depth',0);
for i=1:numel(Props)
prop=Props{i};
Psub=findobj(objPlots,'-property',prop);
Lsub=findobj(objleg,'-property',prop);
n=numel(Lsub);
if n==0, continue; end
assert(n==numel(Psub));
set( Lsub, {prop}, reshape({Psub.(prop)}, [],1) );
end
end
