
Assigning multiple variables a single legend
조회 수: 13 (최근 30일)
이전 댓글 표시
I have script that plots bar charts for different sleep stages across 'trials'. I want to club one stage from each trial and assign it a color in the legend but I'm unable to do so. The script:
%% NREM
hold on;
h1=bar(categorical({'PT5',}),size(PT5_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','b');
h2=bar(categorical({'PT4',}),size(PT4_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','b');
h3=bar(categorical({'PT3',}),size(PT3_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','b');
h4=bar(categorical({'PT2',}),size(PT2_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','b');
h5=bar(categorical({'PT1',}),size(PT1_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','b');
%% REM
h6=bar(categorical({'PT5',}),size(PT5_Changed.REM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','r');
h7=bar(categorical({'PT4',}),size(PT4_Changed.REM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','r');
h8=bar(categorical({'PT3',}),size(PT3_Changed.REM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','r');
h9=bar(categorical({'PT2',}),size(PT2_Changed.REM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','r');
h10=bar(categorical({'PT1',}),size(PT1_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','r');
%% Intermediate
bar(categorical({'PT5',}),size(PT5_Changed.Intermediate_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','g');
bar(categorical({'PT4',}),size(PT4_Changed.Intermediate_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','g');
bar(categorical({'PT3',}),size(PT3_Changed.Intermediate_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','g');
bar(categorical({'PT2',}),size(PT2_Changed.Intermediate_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','g');
bar(categorical({'PT1',}),size(PT1_Changed.Intermediate_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','g');
%% Quiet Wake
bar(categorical({'PT5',}),size(PT5_Changed.Quiet_Wake_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','y');
bar(categorical({'PT4',}),size(PT4_Changed.Quiet_Wake_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','y');
bar(categorical({'PT3',}),size(PT3_Changed.Quiet_Wake_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','y');
bar(categorical({'PT2',}),size(PT2_Changed.Quiet_Wake_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','y');
bar(categorical({'PT1',}),size(PT1_Changed.Quiet_Wake_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','y');
legend('NREM','REM','Intermediate','Quiet Wake')
ylabel('Number of Bouts');xlabel('Rest Periods')
title(strcat('threshold matrix=','[400 30 3 30]',' , ','window size=','5s',',', ' ','overlap=',num2str(0)),'Interpreter','none')
When I plot this, I get the picture shown below. I need all categories to be the color I assigned them. Thanks for helping out!

댓글 수: 0
채택된 답변
Scott MacKenzie
2021년 5월 5일
I suggest you re-organize your data and use a single bar function using the 'stacked' property for barlayout. Do this and it's relatively straight forward to add a legend and control the bar colors.
y1 = randi(5,1,4);
y2 = randi(5,1,4);
y3 = randi(5,1,4);
y4 = randi(5,1,4);
y = [y1' y2' y3' y4'];
dataLabels = {'y1', 'y2', 'y3', 'y4'};
b = bar(y, 'barlayout', 'stacked');
legend(b, dataLabels, 'location', 'northeast');
b(1).FaceColor = [.8 .4 .4];
b(2).FaceColor = [.7 .9 .7];
b(3).FaceColor = [.7 .7 .8];
b(4).FaceColor = [.7 .8 .7];

추가 답변 (2개)
Walter Roberson
2021년 5월 5일
%% NREM
hold on;
h1=bar(categorical({'PT5',}),size(PT5_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','b');
h2=bar(categorical({'PT4',}),size(PT4_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','b');
h3=bar(categorical({'PT3',}),size(PT3_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','b');
h4=bar(categorical({'PT2',}),size(PT2_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','b');
h5=bar(categorical({'PT1',}),size(PT1_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','b');
%% REM
h6=bar(categorical({'PT5',}),size(PT5_Changed.REM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','r');
h7=bar(categorical({'PT4',}),size(PT4_Changed.REM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','r');
h8=bar(categorical({'PT3',}),size(PT3_Changed.REM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','r');
h9=bar(categorical({'PT2',}),size(PT2_Changed.REM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','r');
h10=bar(categorical({'PT1',}),size(PT1_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','r');
%% Intermediate
h31 = bar(categorical({'PT5',}),size(PT5_Changed.Intermediate_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','g');
bar(categorical({'PT4',}),size(PT4_Changed.Intermediate_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','g');
bar(categorical({'PT3',}),size(PT3_Changed.Intermediate_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','g');
bar(categorical({'PT2',}),size(PT2_Changed.Intermediate_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','g');
bar(categorical({'PT1',}),size(PT1_Changed.Intermediate_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','g');
%% Quiet Wake
h41 = bar(categorical({'PT5',}),size(PT5_Changed.Quiet_Wake_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','y');
bar(categorical({'PT4',}),size(PT4_Changed.Quiet_Wake_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','y');
bar(categorical({'PT3',}),size(PT3_Changed.Quiet_Wake_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','y');
bar(categorical({'PT2',}),size(PT2_Changed.Quiet_Wake_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','y');
bar(categorical({'PT1',}),size(PT1_Changed.Quiet_Wake_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','y');
legend([h1, h6, h31, h41], {'NREM','REM','Intermediate','Quiet Wake'})
ylabel('Number of Bouts');xlabel('Rest Periods')
title(strcat('threshold matrix=','[400 30 3 30]',' , ','window size=','5s',',', ' ','overlap=',num2str(0)),'Interpreter','none')
DGM
2021년 5월 5일
There are a number of ways to do this, but you should just be able to pick one object from each group and use it. The trick is to pick the objects explicitly by handle instead of letting legend pick the first N objects it finds in the figure.
% ...
h1 = bar(categorical({'PT1',}),size(PT1_Changed.NREM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','b');
% ...
h2 = bar(categorical({'PT1',}),size(PT1_Changed.REM_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','r');
% ...
h3 = bar(categorical({'PT1',}),size(PT1_Changed.Intermediate_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','g');
% ...
h4 = bar(categorical({'PT1',}),size(PT1_Changed.Quiet_Wake_Bout_Wise_Firing_Rate,1),0.1,'FaceColor','y');
% explicitly specify one object from each group, use cell array of labels
legend([h1 h2 h3 h4],{'NREM','REM','Intermediate','Quiet Wake'})
Also, IDK if it's supposed to be that way, but the line you have assigned to h10 says NREM instead of REM
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!