How to plot a mean line for all subplots

5 次查看(过去 30 天)
I have a code that gives the following subplots
but I need a mean line in the color green and make it look like the following:
So how would I create this green mean line. Thank you so much in advance and my code looks like the following currently
figure
y =Data.StrideTimeIntervals_15minTrial.PD(:,1);
subplot (3,4,1),plot (y, 'r')
title ('PD 1')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 01 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,2);
subplot (3,4,2),plot (y, 'r')
title ('PD 2')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 02 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,3);
subplot (3,4,3),plot (y, 'r')
title ('PD 3')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 03 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,4);
subplot (3,4,4),plot (y, 'r')
title ('PD 4')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 04 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,5);
subplot (3,4,5),plot (y, 'r')
title ('PD 5')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 05 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,6);
subplot (3,4,6),plot (y, 'r')
title ('PD 6')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 06 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,7);
subplot (3,4,7),plot (y, 'r')
title ('PD 7')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 07 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,8);
subplot (3,4,8),plot (y, 'r')
title ('PD 8')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 08 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,9);
subplot (3,4,9),plot (y, 'r')
title ('PD 9')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 09 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,10);
subplot (3,4,10),plot (y, 'r')
title ('PD 10')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 10 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,11);
subplot (3,4,11),plot (y, 'r')
title ('PD 11')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 11 DONE')
y =Data.StrideTimeIntervals_15minTrial.PD(:,12);
subplot (3,4,12),plot (y, 'r')
title ('PD 12')
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp ('PD 12 DONE')

采纳的回答

the cyclist
the cyclist 2019-10-3
编辑:the cyclist 2019-10-3
In each subplot section ...
yline(mean(y),'g')
after you have defined the y value in that section, and done the plot.
See the documentation for yline to see further customizations you can do.
Also, looks like you could make your code much more concise by using a for loop:
figure
for ii = 1:12
y =Data.StrideTimeIntervals_15minTrial.PD(:,ii);
subplot (3,4,ii),plot (y, 'r')
title (sprintf('PD %d',ii))
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp (sprintf('PD %02d',ii))
end
  5 个评论
the cyclist
the cyclist 2019-10-3
编辑:the cyclist 2019-10-3
figure
for ii = 1:12
% y =Data.StrideTimeIntervals_15minTrial.PD(:,ii);
y = 0.8 + 0.4*rand(1,500);
subplot (3,4,ii),plot (y, 'r')
h = yline(mean(y),'g');
set(h,'LineWidth',3)
title (sprintf('PD %d',ii))
xlabel ('ISI #')
ylabel ('ISI (s)')
axis ([0,500,0.8,1.2])
disp (sprintf('PD %02d',ii))
end
works for me.
Notice that I put in some random data in place of your code. So, the line is always going to be very close to the middle, since that is where the mean is.
Here, I made the line wider, too.
timetry2
timetry2 2019-10-3
Okay, yes it works. Thank you so much for the help!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Labels and Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by