Adding Legend of Data Categories
1 view (last 30 days)
Show older comments
I am trying to plot a single-line graph (alongside moving average and baseline) that is divided into multiple data categories mentioned in group_names. My last struggle is to add legend to the graph. Could anyone please help me how I could correctly add all category names from "group_names" with their corresponding color from "colors" into the legend ? The script includes my attempt of adding legend the way I have successfully done it before, however it does not work here.
Any help will be greatly appreciated.
I will post my full plotting script below to provide the idea of how my code works:
baseline_L = 885.63;
y1_L = [0,99,993,937];
... % unnecessary to post here, only 88 other 4-value vectors
y90_L = [2281,1364,0,0];
% GRAPH PLOTTING
y_values = [y1_L; y2_L; y3_L; y4_L; y5_L; y6_L; y7_L; y8_L; y9_L;...
y10_L; y11_L; y12_L; y13_L; y14_L; y15_L; y16_L; y17_L; y18_L; y19_L;...
y20_L; y21_L; y22_L; y23_L; y24_L; y25_L; y26_L; y27_L; y28_L; y29_L;...
y30_L; y31_L; y32_L; y33_L; y34_L; y35_L; y36_L; y37_L; y38_L; y39_L;...
y40_L; y41_L; y42_L; y43_L; y44_L; y45_L; y46_L; y47_L; y48_L; y49_L;...
y50_L; y51_L; y52_L; y53_L; y54_L; y55_L; y56_L; y57_L; y58_L; y59_L;...
y60_L; y61_L; y62_L; y63_L; y64_L; y65_L; y66_L; y67_L; y68_L; y69_L;...
y70_L; y71_L; y72_L; y73_L; y74_L; y75_L; y76_L; y77_L; y78_L; y79_L;...
y80_L; y81_L; y82_L; y83_L; y84_L; y85_L; y86_L; y87_L; y88_L; y89_L;...
y90];
[N,M] = size(y_values);
% GRAPH PLOTTING
colors = {"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
"#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE",...
"#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
'y', 'y', 'y', 'y', 'y',...
"#D95319", "#D95319",...
"#ED33FF", "#ED33FF", "#ED33FF", "#ED33FF", "#ED33FF",...
"#D95319", "#D95319",...
'y', 'y', 'y', 'y', 'y',...
"#D95319", "#D95319",...
"#ED33FF", "#ED33FF", "#ED33FF", "#ED33FF", "#ED33FF",...
"#D95319", "#D95319",...
'y'};
% Define group names for legend
group_names = {'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
'DCZ Pump','DCZ Pump', 'DCZ Pump','DCZ Pump','DCZ Pump','DCZ Pump','DCZ Pump',...
'Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump',...
'No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration',...
'DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection',...
'No Drug Administration','No Drug Administration',...
'Saline Injection','Saline Injection','Saline Injection','Saline Injection','Saline Injection',...
'No Drug Administration','No Drug Administration',...
'DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection',...
'No Drug Administration','No Drug Administration',...
'Saline Injection','Saline Injection','Saline Injection','Saline Injection','Saline Injection',...
'No Drug Administration','No Drug Administration',...
'DCZ Injection'};
% Plotting
figure(3);
hold on;
% Plot baseline
plot([0.5, N+0.5], [baseline, baseline], 'k', 'LineWidth', 2); % Black line with thicker width
h(N+1,1) = plot(NaN,NaN,'k','LineWidth',2); % Dummy line for baseline in legend
% Calculate the mean for each date
mean_values = mean(y_values, 2);
% A moving average with a window size of 5
smooth_mean_values = movmean(mean_values, 5);
% Plot the smooth line
plot(1:N, smooth_mean_values, 'LineWidth', 2, 'Color', 'b');
h(N+2,1) = plot(NaN,NaN,'b','LineWidth',2); % Dummy line for moving average in legend
x_new=0.75:0.25:N+0.5;
y_new=reshape(y_values',[],1);
new_colors=repmat(colors,4,1);
new_colors=reshape(new_colors,[],1);
for i=1:359
plot([x_new(i),x_new(i+1)],[y_new(i),y_new(i+1)],'Color',new_colors{i},'Marker','o','LineStyle','-','MarkerFaceColor',new_colors{i});
end
xticks(1:N); % Placing x-axis ticks for every value
xticklabels({'13.12', '14.12.', '15.12.', '16.12.', '17.12.', '18.12.', '19.12.',...
'20.12.','21.12.','22.12.','23.12.','24.12.','25.12.','26.12.',...
'27.12.','28.12.','29.12.','30.12.','31.12.', ...
'1.1.','2.1.','3.1.','4.1.','5.1.','6.1.','7.1.',...
'8.1.','9.1.','10.1.','11.1.','12.1.','13.1.','14.1.',...
'15.1.','16.1.','17.1.','18.1.','19.1.','20.1.','21.1.',...
'22.1.','23.1.','24.1.','25.1.','26.1.','27.1.','28.1.',...
'29.1.','30.1.','31.1.',...
'1.2.','2.2.','3.2.','4.2.','5.2.','6.2.','7.2.',...
'8.2.','9.2.','10.2.','11.2.','12.2.','13.2.','14.2.',...
'15.2.','16.2.','17.2.','18.2.','19.2.','20.2.','21.2.',...
'22.2.','23.2.','24.2.','25.2.','26.2.','27.2.','28.2.',...
'29.2.'...
'1.3.','2.3.','3.3.','4.3.','5.3.','6.3.','7.3.',...
'8.3.','9.3.','10.3.','11.3.', 'Baseline', 'Moving Average'});
% Add legend
group_names{end+1} = 'Baseline'; % Add baseline to the legend
group_names{end+1} = 'Moving Average'; % Add moving average to the legend
[ugn,idx] = unique(group_names,'stable');
legend(h(idx,1),ugn, 'Location', 'northwest'); % from each group in the legend
% Add title and labels
title('IED Rate During Osmotic Pump and Injection Drug Administration of SK000918', 'FontSize', 19);
xlabel('Date', 'FontSize', 16);
ylabel('6-hour IED Rate', 'FontSize', 16);
hold off;
0 Comments
Accepted Answer
Cris LaPierre
on 7 May 2024
Here's an approach that seemed the most straightforward to me.
I've replaced your y_values array with a randomly generated 90x4 array. Main differences are on how I capture the graphics objects, organze the plotted X and Y data, and inputs to the legend function.
baseline = 885.63;
% GRAPH PLOTTING
y_values = round(2300*rand(90,4));
[N,M] = size(y_values);
% GRAPH PLOTTING
colors = {"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
"#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE",...
"#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
'y', 'y', 'y', 'y', 'y',...
"#D95319", "#D95319",...
"#ED33FF", "#ED33FF", "#ED33FF", "#ED33FF", "#ED33FF",...
"#D95319", "#D95319",...
'y', 'y', 'y', 'y', 'y',...
"#D95319", "#D95319",...
"#ED33FF", "#ED33FF", "#ED33FF", "#ED33FF", "#ED33FF",...
"#D95319", "#D95319",...
'y'};
% Define group names for legend
group_names = {'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
'DCZ Pump','DCZ Pump', 'DCZ Pump','DCZ Pump','DCZ Pump','DCZ Pump','DCZ Pump',...
'Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump',...
'No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration',...
'DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection',...
'No Drug Administration','No Drug Administration',...
'Saline Injection','Saline Injection','Saline Injection','Saline Injection','Saline Injection',...
'No Drug Administration','No Drug Administration',...
'DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection',...
'No Drug Administration','No Drug Administration',...
'Saline Injection','Saline Injection','Saline Injection','Saline Injection','Saline Injection',...
'No Drug Administration','No Drug Administration',...
'DCZ Injection'};
% Plotting
figure(3);
% Plot baseline
h(1) = plot([0.5, N+0.5], [baseline, baseline], 'k', 'LineWidth', 2); % Black line with thicker width
hold on;
% Calculate the mean for each date
mean_values = mean(y_values, 2);
% A moving average with a window size of 5
smooth_mean_values = movmean(mean_values, 5);
% Plot the smooth line
h(2) = plot(1:N, smooth_mean_values, 'LineWidth', 2, 'Color', 'b');
x_new=0.75:0.25:N+0.5;
x_new=reshape(x_new,4,[])';
for i=1:length(y_values)
h(i+2) = plot(x_new(i,:),y_values(i,:),'Color',colors{i},'Marker','o','LineStyle','-','MarkerFaceColor',colors{i});
end
xticks(1:N); % Placing x-axis ticks for every value
xticklabels({'13.12', '14.12.', '15.12.', '16.12.', '17.12.', '18.12.', '19.12.',...
'20.12.','21.12.','22.12.','23.12.','24.12.','25.12.','26.12.',...
'27.12.','28.12.','29.12.','30.12.','31.12.', ...
'1.1.','2.1.','3.1.','4.1.','5.1.','6.1.','7.1.',...
'8.1.','9.1.','10.1.','11.1.','12.1.','13.1.','14.1.',...
'15.1.','16.1.','17.1.','18.1.','19.1.','20.1.','21.1.',...
'22.1.','23.1.','24.1.','25.1.','26.1.','27.1.','28.1.',...
'29.1.','30.1.','31.1.',...
'1.2.','2.2.','3.2.','4.2.','5.2.','6.2.','7.2.',...
'8.2.','9.2.','10.2.','11.2.','12.2.','13.2.','14.2.',...
'15.2.','16.2.','17.2.','18.2.','19.2.','20.2.','21.2.',...
'22.2.','23.2.','24.2.','25.2.','26.2.','27.2.','28.2.',...
'29.2.'...
'1.3.','2.3.','3.3.','4.3.','5.3.','6.3.','7.3.',...
'8.3.','9.3.','10.3.','11.3.', 'Baseline', 'Moving Average'});
% Add legend
group_names{end+1} = 'Baseline'; % Add baseline to the legend
group_names{end+1} = 'Moving Average'; % Add moving average to the legend
[ugn,idx] = unique(group_names([end-1:end 1:end-2]),'stable');
legend(h(idx),ugn,'Location', 'northwest')
% Add title and labels
title('IED Rate During Osmotic Pump and Injection Drug Administration of SK000918', 'FontSize', 19);
xlabel('Date', 'FontSize', 16);
ylabel('6-hour IED Rate', 'FontSize', 16);
hold off;
3 Comments
Cris LaPierre
on 7 May 2024
Edited: Cris LaPierre
on 7 May 2024
The first thing is to make sure your x_new vector contains the correct number of values. I don't know how you determined the current values, but perhaps this?
x_new=0.75:1/M:N+0.5;
Change your reshape input to match the length of your vectors
x_new=reshape(x_new,4,[])';
% ^ replace the 4 with 23
To make it more dynamic, you could use length(y1_L) instead
x_new=reshape(x_new,length(y1_L),[])';
% or
x_new=reshape(x_new,size(y_values,2),[])';
% or just
x_new=reshape(x_new,M,[])';
More Answers (1)
Voss
on 7 May 2024
baseline_L = 885.63;
baseline = baseline_L;
% random data
y_values = 2000*rand(90,4);
[N,M] = size(y_values);
% GRAPH PLOTTING
colors = {"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
"#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE",...
"#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
'y', 'y', 'y', 'y', 'y',...
"#D95319", "#D95319",...
"#ED33FF", "#ED33FF", "#ED33FF", "#ED33FF", "#ED33FF",...
"#D95319", "#D95319",...
'y', 'y', 'y', 'y', 'y',...
"#D95319", "#D95319",...
"#ED33FF", "#ED33FF", "#ED33FF", "#ED33FF", "#ED33FF",...
"#D95319", "#D95319",...
'y'};
% Define group names for legend
group_names = {'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
'DCZ Pump','DCZ Pump', 'DCZ Pump','DCZ Pump','DCZ Pump','DCZ Pump','DCZ Pump',...
'Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump',...
'No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration',...
'DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection',...
'No Drug Administration','No Drug Administration',...
'Saline Injection','Saline Injection','Saline Injection','Saline Injection','Saline Injection',...
'No Drug Administration','No Drug Administration',...
'DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection',...
'No Drug Administration','No Drug Administration',...
'Saline Injection','Saline Injection','Saline Injection','Saline Injection','Saline Injection',...
'No Drug Administration','No Drug Administration',...
'DCZ Injection'};
% Include baseline and MA group names and colors
group_names(end+[1 2]) = {'Baseline','Moving Average'};
colors(end+[1 2]) = {'k','b'};
% unique groups and corresponding colors
[ugn,idx] = unique(group_names,'stable');
uc = colors(idx);
% Plotting
figure(3);
hold on;
% dummy lines for legend
uN = numel(ugn);
h = gobjects(uN,1);
for ii = 1:uN-2
h(ii) = plot(NaN,NaN,'Color',uc{ii},'Marker','o','LineStyle','-','MarkerFaceColor',uc{ii});
end
% Plot baseline
plot([0.5, N+0.5], [baseline, baseline], 'k', 'LineWidth', 2); % Black line with thicker width
h(uN-1,1) = plot(NaN,NaN,'k','LineWidth',2); % Dummy line for baseline in legend
% Calculate the mean for each date
mean_values = mean(y_values, 2);
% A moving average with a window size of 5
smooth_mean_values = movmean(mean_values, 5);
% Plot the smooth line
plot(1:N, smooth_mean_values, 'LineWidth', 2, 'Color', 'b');
h(uN,1) = plot(NaN,NaN,'b','LineWidth',2); % Dummy line for moving average in legend
x_new=0.75:0.25:N+0.5;
y_new=reshape(y_values',[],1);
new_colors=repmat(colors,4,1);
new_colors=reshape(new_colors,[],1);
for i=1:359
plot([x_new(i),x_new(i+1)],[y_new(i),y_new(i+1)],'Color',new_colors{i},'Marker','o','LineStyle','-','MarkerFaceColor',new_colors{i});
end
xticks(1:N); % Placing x-axis ticks for every value
xticklabels({'13.12', '14.12.', '15.12.', '16.12.', '17.12.', '18.12.', '19.12.',...
'20.12.','21.12.','22.12.','23.12.','24.12.','25.12.','26.12.',...
'27.12.','28.12.','29.12.','30.12.','31.12.', ...
'1.1.','2.1.','3.1.','4.1.','5.1.','6.1.','7.1.',...
'8.1.','9.1.','10.1.','11.1.','12.1.','13.1.','14.1.',...
'15.1.','16.1.','17.1.','18.1.','19.1.','20.1.','21.1.',...
'22.1.','23.1.','24.1.','25.1.','26.1.','27.1.','28.1.',...
'29.1.','30.1.','31.1.',...
'1.2.','2.2.','3.2.','4.2.','5.2.','6.2.','7.2.',...
'8.2.','9.2.','10.2.','11.2.','12.2.','13.2.','14.2.',...
'15.2.','16.2.','17.2.','18.2.','19.2.','20.2.','21.2.',...
'22.2.','23.2.','24.2.','25.2.','26.2.','27.2.','28.2.',...
'29.2.'...
'1.3.','2.3.','3.3.','4.3.','5.3.','6.3.','7.3.',...
'8.3.','9.3.','10.3.','11.3.', 'Baseline', 'Moving Average'});
% Add legend
legend(h,ugn, 'Location', 'northwest'); % from each group in the legend
% Add title and labels
title('IED Rate During Osmotic Pump and Injection Drug Administration of SK000918', 'FontSize', 19);
xlabel('Date', 'FontSize', 16);
ylabel('6-hour IED Rate', 'FontSize', 16);
hold off;
See Also
Categories
Find more on Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!