This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Add reference line to plot


hline = refline(___)


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.


collapse all

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 .


Superimpose a least-squares line on the scatter plot.


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.

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.

ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);


Superimpose a least-squares line on the top plot, and a reference line at the mean of the y2 values in the bottom plot.


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

See Also

| |

Introduced before R2006a

Was this topic helpful?