Plotting a Multiple Category Single Line Graph

2 ビュー (過去 30 日間)
Milan
Milan 2024 年 5 月 6 日
コメント済み: Milan 2024 年 5 月 7 日
I am aware, that my question is probably a bit trivial but currently I struggle with the following. Is there anyone who could give me a hint/ help me how to achieve transforming this bar graph plotting into a single line graph, which would be visibly divided by given colors into mentioned categories ? I know that even this bar graph could probably be written more professionally but it works as I needed it to.
Basically I would need now leave baseline and moving average plot as it is but replace bar graph with the line graph with sections divided into given categories distinguished by colors. Thanks in advance for any help !
I will post the full plotting script below to give you the full idea of how it looks and works now.
baseline = 985;
y1 = [0,115,1085,969];
y2 = [1034,846,1037,959];
y3 = [1054,938,1069,690];
y4 = [1126,1283,1119,926];
y5 = [1179,1071,937,815];
y6 = [1562,1065,595,0];
y7 = [0,0,205,0];
y8 = [0,119,1559,936];
y9 = [1442,1382,1128,869];
y10 = [1366,1125,1682,662];
y11 = [1236,1152,1263,455];
y12 = [663,1204,1197,772];
y13 = [567,1135,1177,437];
y14 = [628,1274,1181,560];
y15 = [624,1195,1133,370];
y16 = [701,1210,1182,286];
y17 = [889,913,1236,411];
y18 = [834,1119,1034,514];
y19 = [1076,1213,983,629];
y20 = [679,1253,937,535];
y21 = [899,1166,976,362];
y22 = [581,1026,1153,656];
y23 = [899,109,530,469];
y24 = [1066,854,1116,699];
y25 = [1056,1279,885,803];
y26 = [692,1154,1169,549];
y27 = [796,1072,737,793];
y28 = [964,1027,815,686];
y29 = [1125,997,1009,908];
y30 = [1091,873,1375,1169];
y31 = [2010,2927,102,22];
y32 = [117,201,158,255];
y33 = [767,638,761,813];
y34 = [1003,4478,3059,2173];
y35 = [1172,1387,1166,684];
y36 = [956,1723,1414,1191];
y37 = [1646,2342,1520,1400];
y38 = [1878,1636,2004,2927];
y39 = [3006,3728,3357,3123];
y40 = [1735,3355,1662,0];
y41 = [0,0,0,0];
y42 = [0,0,0,0];
y43 = [0,0,0,0];
y44 = [0,91,2499,2763];
y45 = [3500,3756,4497,4855];
y46 = [4365,5413,4395,5006];
y47 = [4439,6362,5246,4490];
y48 = [3532,3135,10380,7685];
y49 = [9333,9423,7723,6625];
y50 = [8411,8535,7117,5581];
y51 = [6141,4989,6127,4770];
y52 = [4290,5724,4377,5472];
y53 = [4707,6566,5385,3690];
y54 = [2733,4591,4725,3456];
y55 = [6106,2606,2988,1898];
y56 = [1319,7162,6203,6262];
y57 = [8510,8150,8077,5613];
y58 = [6028,6765,5960,3958];
y59 = [5181,5039,6183,4532];
y60 = [5778,6145,8838,5625];
y61 = [7200,7943,9864,5508];
y62 = [7441,6908,9364,4261];
y63 = [6435,7747,7497,5477];
y64 = [4443,3636,6469,3911];
y65 = [6264,6193,8814,5037];
y66 = [6465,7798,9669,6843];
y67 = [6825,9332,8712,6870];
y68 = [1646,2342,1520,1400];
y69 = [6024,3799,6065,4301];
y70 = [7226,6196,7934,5230];
y71 = [9181,7589,10649,8484];
y72 = [9936,8689,8069,7025];
y73 = [9902,8855,10221,5911];
y74 = [10597,11248,10448,9251];
y75 = [8966,11202,9381,7706];
y76 = [5481,7704,5626,6627];
y77 = [7004,8979,15525,10600];
y78 = [10877,10575,11727,7410];
y79 = [8331,9126,11270,7206];
y80 = [8377,8515,8906,5751];
y81 = [9556,12715,12275,7519];
y82 = [10640,8454,12217,11195];
y83 = [7434,9541,10320,4901];
y84 = [7917,10796,10405,7594];
y85 = [12001,6464,11380,5100];
y86 = [10753,7772,9444,6545];
y87 = [11981,5952,14115,5099];
y88 = [9298,8893,11497,6826];
y89 = [6377,8703,9580,9061];
y90 = [5531,2099,0,0];
% GRAPH PLOTTING
y_values = [y1; y2; y3; y4; y5; y6; y7; y8; y9;...
y10; y11; y12; y13; y14; y15; y16; y17; y18; y19;...
y20; y21; y22; y23; y24; y25; y26; y27; y28; y29;...
y30; y31; y32; y33; y34; y35; y36; y37; y38; y39;...
y40; y41; y42; y43; y44; y45; y46; y47; y48; y49;...
y50; y51; y52; y53; y54; y55; y56; y57; y58; y59;...
y60; y61; y62; y63; y64; y65; y66; y67; y68; y69;...
y70; y71; y72; y73; y74; y75; y76; y77; y78; y79;...
y80; y81; y82; y83; y84; y85; y86; y87; y88; y89; y90];
% 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(1);
hold on;
[N,M] = size(y_values);
h = gobjects(N+2,M); % Increase the size by 2 to accommodate baseline and moving average
for i = 1:N
% store the bars created because only certain ones are used in the legend
h(i,:) = bar(i, y_values(i,:), 'FaceColor', colors{i});
end
% 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);
% Smooth the mean values using 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
% Set x-axis labels
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'); % only use the first bar
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;
% Pause for 2 seconds
pause(2);
% Save the plot
saveas(gcf, 'SK000918_6-hour_IED_Rate.png');

採用された回答

sai charan sampara
sai charan sampara 2024 年 5 月 7 日
Hello Milan,
The following code might help you:
baseline = 985;
y1 = [0,115,1085,969];
y2 = [1034,846,1037,959];
y3 = [1054,938,1069,690];
y4 = [1126,1283,1119,926];
y5 = [1179,1071,937,815];
y6 = [1562,1065,595,0];
y7 = [0,0,205,0];
y8 = [0,119,1559,936];
y9 = [1442,1382,1128,869];
y10 = [1366,1125,1682,662];
y11 = [1236,1152,1263,455];
y12 = [663,1204,1197,772];
y13 = [567,1135,1177,437];
y14 = [628,1274,1181,560];
y15 = [624,1195,1133,370];
y16 = [701,1210,1182,286];
y17 = [889,913,1236,411];
y18 = [834,1119,1034,514];
y19 = [1076,1213,983,629];
y20 = [679,1253,937,535];
y21 = [899,1166,976,362];
y22 = [581,1026,1153,656];
y23 = [899,109,530,469];
y24 = [1066,854,1116,699];
y25 = [1056,1279,885,803];
y26 = [692,1154,1169,549];
y27 = [796,1072,737,793];
y28 = [964,1027,815,686];
y29 = [1125,997,1009,908];
y30 = [1091,873,1375,1169];
y31 = [2010,2927,102,22];
y32 = [117,201,158,255];
y33 = [767,638,761,813];
y34 = [1003,4478,3059,2173];
y35 = [1172,1387,1166,684];
y36 = [956,1723,1414,1191];
y37 = [1646,2342,1520,1400];
y38 = [1878,1636,2004,2927];
y39 = [3006,3728,3357,3123];
y40 = [1735,3355,1662,0];
y41 = [0,0,0,0];
y42 = [0,0,0,0];
y43 = [0,0,0,0];
y44 = [0,91,2499,2763];
y45 = [3500,3756,4497,4855];
y46 = [4365,5413,4395,5006];
y47 = [4439,6362,5246,4490];
y48 = [3532,3135,10380,7685];
y49 = [9333,9423,7723,6625];
y50 = [8411,8535,7117,5581];
y51 = [6141,4989,6127,4770];
y52 = [4290,5724,4377,5472];
y53 = [4707,6566,5385,3690];
y54 = [2733,4591,4725,3456];
y55 = [6106,2606,2988,1898];
y56 = [1319,7162,6203,6262];
y57 = [8510,8150,8077,5613];
y58 = [6028,6765,5960,3958];
y59 = [5181,5039,6183,4532];
y60 = [5778,6145,8838,5625];
y61 = [7200,7943,9864,5508];
y62 = [7441,6908,9364,4261];
y63 = [6435,7747,7497,5477];
y64 = [4443,3636,6469,3911];
y65 = [6264,6193,8814,5037];
y66 = [6465,7798,9669,6843];
y67 = [6825,9332,8712,6870];
y68 = [1646,2342,1520,1400];
y69 = [6024,3799,6065,4301];
y70 = [7226,6196,7934,5230];
y71 = [9181,7589,10649,8484];
y72 = [9936,8689,8069,7025];
y73 = [9902,8855,10221,5911];
y74 = [10597,11248,10448,9251];
y75 = [8966,11202,9381,7706];
y76 = [5481,7704,5626,6627];
y77 = [7004,8979,15525,10600];
y78 = [10877,10575,11727,7410];
y79 = [8331,9126,11270,7206];
y80 = [8377,8515,8906,5751];
y81 = [9556,12715,12275,7519];
y82 = [10640,8454,12217,11195];
y83 = [7434,9541,10320,4901];
y84 = [7917,10796,10405,7594];
y85 = [12001,6464,11380,5100];
y86 = [10753,7772,9444,6545];
y87 = [11981,5952,14115,5099];
y88 = [9298,8893,11497,6826];
y89 = [6377,8703,9580,9061];
y90 = [5531,2099,0,0];
% GRAPH PLOTTING
y_values = [y1; y2; y3; y4; y5; y6; y7; y8; y9;...
y10; y11; y12; y13; y14; y15; y16; y17; y18; y19;...
y20; y21; y22; y23; y24; y25; y26; y27; y28; y29;...
y30; y31; y32; y33; y34; y35; y36; y37; y38; y39;...
y40; y41; y42; y43; y44; y45; y46; y47; y48; y49;...
y50; y51; y52; y53; y54; y55; y56; y57; y58; y59;...
y60; y61; y62; y63; y64; y65; y66; y67; y68; y69;...
y70; y71; y72; y73; y74; y75; y76; y77; y78; y79;...
y80; y81; y82; y83; y84; y85; y86; y87; y88; y89; 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(1);
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);
% Smooth the mean values using 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');
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="-");
end
%plot(x_new,y_new,"k");
h(N+2,1) = plot(NaN,NaN,'b','LineWidth',2); % Dummy line for moving average in 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;
% Pause for 2 seconds
pause(2);
  2 件のコメント
Milan
Milan 2024 年 5 月 7 日
Thanks a lot, this is surely pretty much what I tried to accomplish.
Milan
Milan 2024 年 5 月 7 日
Hi,
by any chance, could I please ask one more question ?
If my vectors in y_values would not be of 4 values each but 23 like the ones below, how should be the code edited for it to work ? I tried a few things and thought it will be easy but I ran to so many errors along the way...
y1_L = [222 224 134 162 88 174 209 190 132 150 71 114 182 176 319 142 72 90 129 127 132 139 129];
y2_L = [268 16 185 225 47 334 229 139 254 183 255 181 132 133 215 177 239 186 90 172 209 206 208];

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeApplications についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by