How can I have one unstacked bar in an otherwise stacked bar graph
5 views (last 30 days)
Show older comments
Cameron Murray
on 30 Sep 2016
Commented: Cameron Murray
on 30 Sep 2016
I have a stacked bar graph with several values and I want a 6th value that is not stacked. In the graph below I want the "shear demand at M_n" bar to be gray for example, and have its own spot in the legend. I can't figure out how to separate it from the other values though. Here is the code to produce the figure below:
estcap=[157.75 34.07; 94.34 84.57; 135.73 72.55; 70.16 13.07; 109.76 31.16; 0 146.15];
actcap=[159.4];
figure('name','A1, a/d = 2.5');
barg=bar(estcap, 'stacked','linewidth',1.5);
hold on
plot(xlim,[actcap actcap], '-k','linewidth',2);
legend('Steel Contribution','Concrete Contribution','Shear Demand at Failure','location','northoutside');
ylabel('Capacity (kips)');
Labels = {'1973-STD','ACI', '2012-SIMP', '2012-GEN', '2004-AASHTO','Shear Demand at M_n'};
set(gca, 'xtick',1:6, 'XTickLabel', Labels,'XTickLabelRotation',45);
colormap gray
set(gca,'Linewidth',1.5);
hold off
I hope that makes sense, thanks in advance for any help.

0 Comments
Accepted Answer
Kelly Kearney
on 30 Sep 2016
I would simply call add a second bar object, making sure to specify the x-locations for each bar. I also prefer to pass specific handles to the legend command; it's not always necessary but it gives you more control than assuming legend will grab the right objects in the right order:
estcap=[157.75 34.07; 94.34 84.57; 135.73 72.55; 70.16 13.07; 109.76 31.16; 0 146.15];
actcap = 150;
figure('name','A1, a/d = 2.5');
axes;
hold on;
barg = bar(1:5, estcap(1:end-1,:), 'stacked','linewidth',1.5);
bar2 = bar(6, estcap(end,2), 'linewidth', 1.5, 'facecolor', ones(1,3)*0.8);
ln = plot(xlim,[actcap actcap], '-k','linewidth',2);
legend([barg bar2 ln], 'black', 'white', 'gray', 'line', 'location','northoutside');
ylabel('Capacity (kips)');
Labels = {'1973-STD','ACI', '2012-SIMP', '2012-GEN', '2004-AASHTO','Shear Demand at M_n'};
set(gca, 'xtick',1:6, 'XTickLabel', Labels,'XTickLabelRotation',45);
colormap gray
set(gca,'Linewidth',1.5);
hold off
More Answers (0)
See Also
Categories
Find more on Discrete Data Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!