Add trend line to scatter plot with years in the X-axis
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Anwesha Sharma
am 20 Okt. 2022
Kommentiert: Star Strider
am 21 Okt. 2022
I have a scatter plot with Date in the X-axis and length in Y-axis. I have been trying to add a trendline to the plot but I think because of the Date in the X-axis, polyfit or polyval are not working. Is there another way to do it?
scatter(Date, Rift)
c = polyfit(Date Rift, 1)
OR
f=fit(Date, Rift,'poly1')

0 Kommentare
Akzeptierte Antwort
Star Strider
am 20 Okt. 2022
I am not certain what the data are.
Assuming originally a datetime array and a double vector, try something lilke this —
Date = datetime(2004,01,01)+calmonths(0:1:12*16);
Rift = sin(2*pi*(1:numel(Date))/12);
[c,S,mu] = polyfit(datenum(Date), Rift, 1);
trendline = polyval(c,datenum(Date),S,mu);
figure
plot(Date, Rift)
hold on
plot(Date, trendline, '-r')
hold off
grid
.
2 Kommentare
Star Strider
am 21 Okt. 2022
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Calendar finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
