Special legend for special bar plot

Here's the code for my bar plot:
ctgroup = reshape(Ct(2:end),5,[]);
ctgroup = [nan,Ct(1),nan;ctgroup];
figure, b = bar(1:6,ctgroup,'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;
ylabel('C_t','fontsize',14)
The plot is attached. I want to add a legend to this plot that shows the first four bar colors (that's each color once, or black, purple, dark blue, light blue), and nothing more. How can it be done?

 采纳的回答

See if the approach in How to draw bar graph of different colors ? will do what you want.

5 个评论

Thanks! I think this will work, but I'm not quite there. My color scheme is a bit complicated as you can see in my original code and the attached plot and the bars are grouped. I'm having trouble taking the example in your link and recreating what I had but in a loop now so that I can add the legend. Could you help with that?
I do not have any of your data, nor do I understand in any detail what you want to do with it.
The purpose of that Answer was to create the bars and add an appropriate legend, and it worked as requested. It will likely be necessary for you to experiment with that code to get the desired result.
I've added the necessary data below. It took some doing previously to color the bars the way I wanted. Now that the bars need to be created in a loop to make the legend, the indexing is different and I'm struggling to recreate the colors I had previously as shown in the attached plot or using the code provided above. I tried this:
ctgroup = [NaN, 0.9377, NaN;...
0.6716, 0.5600, 0.4188;...
0.6675, 0.5751, 0.4253;...
0.7358, 0.6248, 0.4590;...
0.6302, 0.5334, 0.3761;...
1.1973, 1.2254, 1.4879];
barcols = {[1 1 1], [0 0 0], [1 1 1];...
[0.4940 0.1840 0.5560], [0 0.4470 0.7410], [0.3010 0.7450 0.9330];...
[0.4940 0.1840 0.5560], [0 0.4470 0.7410], [0.3010 0.7450 0.9330];...
[0.4940 0.1840 0.5560], [0 0.4470 0.7410], [0.3010 0.7450 0.9330];...
[0.4940 0.1840 0.5560], [0 0.4470 0.7410], [0.3010 0.7450 0.9330];...
[0.4940 0.1840 0.5560], [0 0.4470 0.7410], [0.3010 0.7450 0.9330]};
figure, hold on
for i = 1:length(ctgroup)
b{i} = bar(i,ctgroup(i,:),'FaceColor','Flat');
end
for i = 1:length(barcols)
for j = 1:3
b{i}(j).CData(i,:) = barcols{i,j};
end
end
% the legend that I want to show corresponding to black, purple, dark blue,
% and light blue:
legend('Baseline','10% Derate','20% Derate','40% Derate')
After the first i of the second loop, though, I get the error that "CData must be an RGB triplet, a scalar, an M-by-1 vector the same length as X, or an M-by-3 matrix." I didn't have this error before, so I'm not sure why now.
Also, to the original question, I can create a legend now, but only when I accept the default colors, which isn't what I'm hoping for. I want the legend to be one entry for each color I use (not including the white).
Got it.
figure, hold on
for i = 1:length(ctgroup)
b{i} = bar(i,ctgroup(i,:),'FaceColor','flat');
end
b{1}(1).HandleVisibility = 'off';
b{1}(3).HandleVisibility = 'off';
for i = 1:length(b)
for j = 1:3
b{i}(j).FaceColor = barcols{i,j};
end
end
legend('Baseline','10% Derate','20% Derate','40% Derate')
I knew you could do it!
Thank you!

请先登录,再进行评论。

更多回答(0 个)

类别

产品

版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by