How can I fit a scatter plot?
3 views (last 30 days)
Show older comments
In MATLAB, I created a scatter plot of my data with this code:
Lac = readmatrix("Lac.xlsx", "Range", "B2:Y8");
t = readmatrix("Lac.xlsx", "Range", "A2:A8");
t is a 7x1 matrix; Lac is a 7x24 matrix.
How can I fit a line to this scatter plot?
Is your goal to fit all the data together or to fit each of the 7 groups individually?
This demo shows how to plot a linera fit using the entire data.
t = rand(7,1)*10;
Lac = rand(7,24)+linspace(0,2,7)';
tm = repelem(t,1,size(Lac,2));
If you have the Curve Fitting Toolbox: fit()
mdl = fit(tm(:),Lac(:),'Poly1')
Without the Curve Fitting Toolbox: polyfit()
coefs = polyfit(tm,Lac,1)
More Answers (1)
Anton Kogios on 23 Mar 2023
If you want to fit a line to the data as a whole, I think this should work:
t = 1:7;
Lac = randi(10,[7,24]);
[coeffs] = polyfit(t,mean(Lac'),1)
where the last argument for polyfit is the degree of the fit that you want.
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!