Complicated grouped bar legend

36 visualizzazioni (ultimi 30 giorni)
Daniel
Daniel il 26 Ott 2020
Commentato: Daniel il 2 Nov 2020
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!

Risposte (1)

Divija Aleti
Divija Aleti il 30 Ott 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:
  1 Commento
Daniel
Daniel il 2 Nov 2020
This helps, but, if you look at my figure, I've changed the color of one of the bars in the first group so it's different from all the other groups. In your example, imagine the red bar was black and you wanted it to come first in the legend without changing or getting rid of anything you already have. How would you do that?

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by