refline

Add reference line to plot

Syntax

refline(m,b)
refline(coeffs)
refline
refline(ax,___)
hline = refline(___)

Description

refline(m,b) adds a reference line with slope m and intercept b to the current axes.

refline(coeffs), where coeffs is a two-element coefficient vector, adds the line

   y = coeffs(1)*x + coeffs(2) 

to the figure.

refline with no input arguments is equivalent to lsline.

refline(ax,___) adds a reference line to the plot in the axis specified by ax, using any of the previous syntaxes.

hline = refline(___) returns the handle hline to the line.

Examples

expand all

Add a Reference Line at the Mean

Generate sample data for independent variable x and a dependent variable y .

x = 1:10;
y = x + randn(1,10);

Create a scatter plot of x and y .

scatter(x,y,25,'b','*')

Superimpose a least-squares line on the scatter plot.

lsline

Add a reference line at the mean of the scatter and its least-squares line.

mu = mean(y);
hline = refline([0 mu]);
hline.Color = 'r';

The red line shows the reference line at the mean of data.

Specify Axes for Least-Squares and Reference Lines

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])

See Also

| |

Was this topic helpful?