How to add trendline to scatterplot?

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?

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 12 月 27 日

6 投票

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 件のコメント

Ani Asoyan
Ani Asoyan 2020 年 12 月 27 日
Thank you so much !!
Ameer Hamza
Ameer Hamza 2020 年 12 月 27 日
I am glad to be of help!

サインインしてコメントする。

その他の回答 (1 件)

Dima
Dima 2024 年 3 月 17 日
編集済み: Dima 2024 年 3 月 17 日

0 投票

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.

カテゴリ

タグ

質問済み:

2020 年 12 月 27 日

編集済み:

2024 年 3 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by