Plotting a Multiple Category Line Graph of Longer Vectors

1 次查看(过去 30 天)
I am aware, that my question is probably a bit trivial but currently I struggle with the following. Is there anyone who could please give me a hint/ help me to make this plotting script work with the set of vectors, each of the length of 23 values as displayed below ?:
y1_L = [0 0 0 0 0 0 0 0 0 0 1 98 92 129 204 147 228 193 176 204 184 197 176];
y2_L = [196 192 201 155 38 212 171 145 112 131 137 107 195 153 186 149 130 173 187 214 132 190 205];
...and so on til y90_L
Currently my script works well when each vector is 4-value long but I run to many indexing errors when I tried to adapt it to accept 23-value-long vectors. I thought it would be easy, but I may need a little hint how to do it correctly.
I tried replacing
x_new=reshape(x_new,4,[])';
Unrecognized function or variable 'x_new'.
with
x_new=reshape(x_new,M,[])';
to make the script more dynamic, however the indexing errors still persist.
Thanks in advance for any help !
I am posting the full plotting script below to give you the full idea of how it looks and works now:
baseline_L = 885.625;
y1_L = [0,99,993,937];
y2_L = [994,803,986,928];
y3_L = [1004,866,981,656];
y4_L = [1075,1241,1082,885];
y5_L = [1131,1016,876,713];
y6_L = [1450,917,479,0];
y7_L = [0,0,176,0];
y8_L = [0,113,1441,782];
y9_L = [1348,1325,1034,820];
y10_L = [1291,1044,1626,635];
y11_L = [1190,1098,1202,413];
y12_L = [612,1131,1147,717];
y13_L = [515,1060,1114,411];
y14_L = [594,1199,1117,499];
y15_L = [571,1119,1044,336];
y16_L = [653,1140,1118,251];
y17_L = [832,839,1167,380];
y18_L = [757,1053,977,462];
y19_L = [996,1157,930,589];
y20_L = [648,1163,878,493];
y21_L = [831,1105,899,329];
y22_L = [537,980,1075,627];
y23_L = [850,109,511,438];
y24_L = [994,809,1015,587];
y25_L = [952,1172,832,755];
y26_L = [638,1108,1125,516];
y27_L = [747,1028,697,723];
y28_L = [915,972,737,626];
y29_L = [1050,965,963,853];
y30_L = [1047,818,1314,1135];
y31_L = [1923,2369,5,1];
y32_L = [47,99,89,189];
y33_L = [560,506,565,474];
y34_L = [468,2408,1587,648];
y35_L = [795,1060,852,434];
y36_L = [712,1387,1124,1054];
y37_L = [1452,2054,1321,1233];
y38_L = [1662,1431,1716,2703];
y39_L = [2816,3449,3061,2963];
y40_L = [1571,3028,1498,0];
y41_L = [0,0,0,0];
y42_L = [0,0,0,0];
y43_L = [0,0,0,0];
y44_L = [0,75,2163,2509];
y45_L = [3096,3256,4023,4483];
y46_L = [3914,5034,3964,4446];
y47_L = [3878,5344,4198,3577];
y48_L = [2499,2146,8475,6221];
y49_L = [7553,7427,5527,4408];
y50_L = [6025,5994,4317,3518];
y51_L = [3965,2980,4205,3093];
y52_L = [2495,3760,2701,3985];
y53_L = [3321,4737,3671,2495];
y54_L = [1770,3439,3448,2363];
y55_L = [4403,2082,2596,1183];
y56_L = [741,5151,3940,4389];
y57_L = [6314,5966,5104,3374];
y58_L = [4084,4464,3522,1918];
y59_L = [2873,2837,3235,2700];
y60_L = [3139,3570,5720,3273];
y61_L = [4589,4761,6301,3124];
y62_L = [5318,4092,6608,2952];
y63_L = [4507,5399,4488,3598];
y64_L = [3051,2377,3818,2158];
y65_L = [3253,3512,5322,3086];
y66_L = [4313,4979,6335,4526];
y67_L = [4858,5774,5007,4348];
y68_L = [3699,5814,3952,2731];
y69_L = [3697,2677,2468,2298];
y70_L = [4937,4445,5581,3829];
y71_L = [7477,6411,7668,5994];
y72_L = [7193,6375,5189,4807];
y73_L = [7672,6376,7115,3861];
y74_L = [8200,8190,7790,7055];
y75_L = [6029,8568,6788,5703];
y76_L = [3228,5185,3164,4991];
y77_L = [5106,7152,12818,8784];
y78_L = [8606,8061,8948,5186];
y79_L = [5336,6639,8553,5109];
y80_L = [5006,6047,6366,3279];
y81_L = [6716,9787,9124,5216];
y82_L = [7384,5237,9354,9008];
y83_L = [4726,6851,7351,2927];
y84_L = [4986,8428,7606,5448];
y85_L = [9119,4164,8406,3037];
y86_L = [7854,5099,5741,3204];
y87_L = [8788,2968,10813,2588];
y88_L = [5882,5748,8276,4469];
y89_L = [3040,5431,6014,6507];
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_L];
[N,M] = size(y_values);
% No Drug Administration -> "#33FFD1"
% DCZ Pump -> "#4DBEEE"
% Saline Pump -> "#D95319"
% DCZ Injection -> "#D133FF"
% Saline Injection -> "#EDB120"
% GRAPH PLOTTING
colors = {"#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
"#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
"#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
"#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
"#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#D95319",...
"#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#EDB120", "#EDB120",...
"#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
"#D133FF", "#D133FF", "#D133FF", "#D133FF", "#D133FF",...
"#33FFD1", "#33FFD1",...
"#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120",...
"#33FFD1", "#33FFD1",...
"#D133FF", "#D133FF", "#D133FF", "#D133FF", "#D133FF",...
"#33FFD1", "#33FFD1",...
"#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120",...
"#33FFD1", "#33FFD1",...
"#D133FF"};
% 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_L, baseline_L], '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 (SK000918)', 'FontSize', 19);
xlabel('Date', 'FontSize', 16);
ylabel('6-hour IED Rate', 'FontSize', 16);
hold off;
  4 个评论
Voss
Voss 2024-5-8
编辑:Voss 2024-5-8
length of an array is the size of the array's longest dimension.
For example, when y_values is of size 90-by-23, length(y_values) is 90, and when y_values is of size 6-by-23, length(y_values) is 23.
If you use for i=1:length(y_values) and use i as a row index in y_values, you'll get an "index exceeds ..." error when the number of columns is greater than the number of rows.
So instead of
for i=1:length(y_values)
you really meant
for i=1:size(y_values,1)
.
Milan
Milan 2024-5-8
Thank you, it works just fine now. I really did not realize the indexing logic you presented so I very much appreciate your help !

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2024-5-8
baseline = 885.63;
% GRAPH PLOTTING
y_values = round(2300*rand(90,23));
[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=linspace(0.75,N+0.5,N*M);
x_new=reshape(x_new,M,[])';
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;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by