Stacked Bar and Legend Handles

1 view (last 30 days)
Matlab2010
Matlab2010 on 28 Jun 2012
Using 2010B.
I say:
h1=bar(x1, y1, 'r', 'stack');
hold all;
h2=bar(x2, y2, 'b', 'stack');
[legend_h, object_h, plot_h, text_strings] = legend([h1 h2],'s1','s2');
When I inspect the plot I see that the colored boxes in the legend are both red in color.
I have just spent ages trying to play with all the children etc but to no avail.
Please can someone tell me how to change the color of the rectangle inside the legend box.
thanks!

Accepted Answer

Tom
Tom on 28 Jun 2012
Edited: Tom on 28 Jun 2012
I'll have a stab at:
h1=bar(x1, y1, 'r', 'stack');
hg1=hggroup;
set(h1,'Parent',hg1)
hold all;
h2=bar(x2, y2, 'b', 'stack');
hg2=hggroup;
set(h2,'Parent',hg2)
set(get(get(hg1,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','on');
set(get(get(hg2,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','on');
[legend_h, object_h, plot_h, text_strings] = legend('s1','s2');
This groups each bar plot -makes it a hggroup- so that each hggroup counts as one item on the legend. The LegendInformation stuff determines whether that object appears in the legend

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!