How to add trendline to scatterplot?

855 Ansichten (letzte 30 Tage)
Ani Asoyan
Ani Asoyan am 27 Dez. 2020
Bearbeitet: Dima am 17 Mär. 2024
hi, I have 2 variables with data, like
x=2 1 4 68 4 5
y=5 4 2 23 21 50
I want to add trendline to the scatter plot. how can I do that?

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 27 Dez. 2020
Something like this
x = [2 1 4 68 4 5]
y = [5 4 2 23 21 50]
p = polyfit(x, y, 1);
px = [min(x) max(x)];
py = polyval(p, px);
scatter(x, y, 'filled')
hold on
plot(px, py, 'LineWidth', 2);
  2 Kommentare
Ani Asoyan
Ani Asoyan am 27 Dez. 2020
Thank you so much !!
Ameer Hamza
Ameer Hamza am 27 Dez. 2020
I am glad to be of help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Dima
Dima am 17 Mär. 2024
Bearbeitet: Dima am 17 Mär. 2024
x = [2 1 4 8 4 5]
y = [5 4 2 53 21 50]
p = polyfit(x, y, 1);
px = [min(x) max(x)];
py = polyval(p, px);
scatter(x, y, 'filled')
lsline
lsline command.

Community Treasure Hunt

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

Start Hunting!

Translated by