Complicated grouped bar legend
37 views (last 30 days)
Show older comments
Here's my plot code:
cpgroup = reshape(Cp(2:end),5,[]);
cpgroup = [nan,Cp(1),nan;cpgroup];
figure, b = bar(1:6,cpgroup,'FaceColor','flat','HandleVisibility','off'); hold on, b(2).CData(1,:) = [0 0 0];
b(1).CData(2,:) = [0.4940 0.1840 0.5560]; b(1).CData(3,:) = [0.4940 0.1840 0.5560]; b(1).CData(4,:) = [0.4940 0.1840 0.5560];
b(1).CData(5,:) = [0.4940 0.1840 0.5560]; b(1).CData(6,:) = [0.4940 0.1840 0.5560];
b(2).CData(2,:) = [0 0.4470 0.7410]; b(2).CData(3,:) = [0 0.4470 0.7410]; b(2).CData(4,:) = [0 0.4470 0.7410];
b(2).CData(5,:) = [0 0.4470 0.7410]; b(2).CData(6,:) = [0 0.4470 0.7410];
b(3).CData(2,:) = [0.3010 0.7450 0.9330]; b(3).CData(3,:) = [0.3010 0.7450 0.9330]; b(3).CData(4,:) = [0.3010 0.7450 0.9330];
b(3).CData(5,:) = [0.3010 0.7450 0.9330]; b(3).CData(6,:) = [0.3010 0.7450 0.9330];
cases = {'Baseline','cst Pitch','cst TSR','max RR','min C_t','max C_t'};
set(gca,'xticklabels',cases); ax = gca; ax.XTick = 1:6;
line([0.5 6.5],[0.9*Cp(1) 0.9*Cp(1)],'color','k','linestyle','--')
line([0.5 6.5],[0.8*Cp(1) 0.8*Cp(1)],'color','k','linestyle','-.')
line([0.5 6.5],[0.6*Cp(1) 0.6*Cp(1)],'color','k','linestyle',':')
ylabel('C_P'), legend('10% Derate','20% Derate','40% Derate')
axis([0.5 6.5 0 0.6])
And my plot is attached. It was a lot to get this far. Now, I'd really like for the legend to contain each bar color once in the order they appear from left to right. I don't know how to resolve that with the modifications I've made to the first group of bars in order to get one bar there that's a different color from the others. To be clear, I'd like the legend to show the black bar, then purple, dark blue, and light blue, and then the three different lines. Thanks!
0 Comments
Answers (1)
Divija Aleti
on 30 Oct 2020
Hi Daniel,
Have a look at the following example to understand how to show bars and line plots in a particular order in the legend.
x = [1 2 3];
vals = [6 26 20; 3 23 18; 2 11 15];
b = bar(x,vals);
l1 = yline(11,'color','k','linestyle','--');
l2 = yline(19,'color','k','linestyle','-.');
l3 = yline(16,'color','k','linestyle',':');
legend([b(1),b(2),b(3),l1,l2,l3],{'p','q','r','l','m','n'});
Output:
See Also
Categories
Find more on Legend in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!