Assigning multiple variables a single legend
이전 댓글 표시
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!

채택된 답변
추가 답변 (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
카테고리
도움말 센터 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
