フィルターのクリア

Add trend line to a scatter plot

50 ビュー (過去 30 日間)
Beatriz Sanchez
Beatriz Sanchez 2018 年 9 月 27 日
編集済み: Adam Danz 2020 年 4 月 5 日
Hi, I'm ploting some data in a scatter and I want to add a trending line to the plot, but the imput x and y for the scatter are vectors and the fit function take x and y as matrix, how can I do this, my code below:
vec= [-180, -90, 0, 90, 180]
x_mean= randi (1000, 3, 5)
for i= 1:3
hold on
scatter (vec, x_mean (i, :), 'b')
end
I want to add one trend line that consider ALL of the values in the scatter. Need help. Thanks
  2 件のコメント
Adam Danz
Adam Danz 2018 年 9 月 27 日
A trend line (like lsline) is usually understood as a regression line and it would definitely not connect all of the values in the scatter. So, is a trend line really what you want?
You could use plot(vect, x_mean) to connect the series of lines but that would result in 3 lines since there are 3 rows in x_mean.
Beatriz Sanchez
Beatriz Sanchez 2018 年 9 月 27 日
I don't want to exactly connect them, I want a trend line that consider all of them

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

回答 (1 件)

Adam Danz
Adam Danz 2018 年 9 月 27 日
編集済み: Adam Danz 2020 年 4 月 5 日
Use coef = polyfit(x,y) to compute the regression coefficients. Then use refline(coef(1),coef(2)) to plot the regression line.
coef = polyfit(repmat(vec, size(x_mean,1), 1), x_mean, 1);
h = refline(coef(1), coef(2));
Alternatively, you can use lsline(ax) to add the least squares regression line to each set of data within the axes without needing to compute the regression coefficients.

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by