Plotting mean across a graph

286 visualizzazioni (ultimi 30 giorni)
Vaughn Beeson
Vaughn Beeson il 17 Nov 2020
Risposto: Steven Lord il 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';
[X, Y]=readvars('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)

Risposte (2)

Star Strider
Star Strider il 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
Steven Lord il 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))

Community Treasure Hunt

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

Start Hunting!

Translated by