
Assigning multiple variables a single legend
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
shekhar narayanan
el 5 de Mayo de 2021
Comentada: shekhar narayanan
el 6 de Mayo de 2021
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 comentarios
Respuesta aceptada
Scott MacKenzie
el 5 de Mayo de 2021
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];

Más respuestas (2)
Walter Roberson
el 5 de Mayo de 2021
%% 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
el 5 de Mayo de 2021
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
Ver también
Categorías
Más información sobre Graphics Object Properties en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!