Documentation

lsline

添加最小二乘直线scatter plot

Syntax

lsline
lsline(ax)
h = lsline(___)

Description

lslinesuperimposes a least-squares line on each scatter plot in the current axes. Scatter plots are produced by the MATLAB®scatterandplotfunctions. Data points connected with solid, dashed, or dash-dot lines (“- - -”,'--', or'.-') are not considered to be scatter plots bylsline, and are ignored.

lsline(ax)superimposes a least-squares line on the scatter plot in axisax.

h = lsline(___)returns a column vector of handleshto the least-squares lines, using any of the previous syntaxes.

Examples

collapse all

Generate three sets of sample data and plot on the same figure.

x = 1:10; rngdefault;% For reproducibilityfigure; y1 = x + randn(1,10); scatter(x,y1,25,'b','*') holdony2 = 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.

lsline

Define the x-variable and two different y-variables to use for the plots.

rngdefault% For reproducibilityx = 1:10; y1 = x + randn(1,10); y2 = 2*x + randn(1,10);

Defineax1as the top half of the figure, andax2as the bottom half of the figure. Create the first scatter plot on the top axis usingy1, and the second scatter plot on the bottom axis usingy2.

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 they2values in the bottom plot.

lsline(ax1) mu = mean(y2); refline(ax2,[0 mu])

Introduced before R2006a

Was this topic helpful?