Add least-squares line to scatter plot
h = lsline(___)
lsline superimposes a least-squares line on each scatter plot in the current axes. Scatter plots are produced by the MATLAB® scatter and plot functions. Data points connected with solid, dashed, or dash-dot lines ('-', '--', or '.-') are not considered to be scatter plots by lsline, and are ignored.
lsline(ax) superimposes a least-squares line on the scatter plot in axis ax.
h = lsline(___) returns a column vector of handles h to the least-squares lines, using any of the previous syntaxes.
Generate three sets of sample data and plot on the same figure.
x = 1:10; rng default; % For reproducibility figure; y1 = x + randn(1,10); scatter(x,y1,25,'b','*') hold on y2 = 2*x + randn(1,10); plot(x,y2,'mo') y3 = 3*x + randn(1,10); plot(x,y3,'rx:')
Add a least-squares line for each set of sample data.
Define the x-variable and two different y-variables to use for the plots.
rng default % For reproducibility x = 1:10; y1 = x + randn(1,10); y2 = 2*x + randn(1,10);
Define ax1 as the top half of the figure, and ax2 as the bottom half of the figure. Create the first scatter plot on the top axis using y1, and the second scatter plot on the bottom axis using y2.
figure ax1 = subplot(2,1,1); ax2 = subplot(2,1,2); scatter(ax1,x,y1) scatter(ax2,x,y2)
Superimpose a least-squares line on the top plot, and a reference line at the mean of the y2 values in the bottom plot.
lsline(ax1) mu = mean(y2); refline(ax2,[0 mu])