# Plotting mean across a graph

18 views (last 30 days)
Vaughn Beeson on 17 Nov 2020
Answered: Steven Lord on 17 Nov 2020
Should be an easy question, but I'm having issues plotting a flat line across this 52x2 array, X being column 1 with datetime and Y being values from 0-100. The graph itself shows, but not the mean. I know that having 3 plot functions seems redundant but only the second one works and I dont know why
file='SearchesForFullMoon_Year.xlsx';
Ymean=mean(Y,'all');
plot(X, Ymean,'r','LineWidth',1.5)
plot(X, Y, 'k','LineWidth',1.5)
hold on;
plot(Ymean, 'r','LineWidth',1.5)

Star Strider on 17 Nov 2020
Try this:
plot(xlim, [1 1]*Ymean, 'r','LineWidth',1.5)
The ‘Ymean’ value should be a constant, so it is necessary to plot it as a vector by multiplying it by [1 1].
Without your data to test this with, I am posting this as UNTESTED CODE. It should work.

Steven Lord on 17 Nov 2020
If I understand what you want to do correctly, use the yline function.
x = datetime('today') + days(0:9);
y = 10*rand(size(x));
plot(x, y)
yline(mean(y))