Add reference line to plot
refline( adds a
reference line to the plot in the axis specified by
any of the input arguments in the previous syntaxes.
Generate sample data for an independent variable
x and a dependent variable
x = 1:10; y = x + randn(1,10);
Create a scatter plot of
Superimpose a least-squares line on the scatter plot.
Add a reference line at the mean of the scatter plot.
mu = mean(y); hline = refline([0 mu]); hline.Color = 'r';
The red line is the reference line at the mean of the 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);
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
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) % This is equivalent to refline(ax1) mu = mean(y2); refline(ax2,[0 mu])
m— Slope of reference line
Slope of the reference line, specified as a numeric scalar. The function
m to define the line
y = m*x + b.
b— Intercept of reference line
Intercept of the reference line, specified as a numeric scalar. The
b to define the line
y = m*x + b.
coeffs— Linear coefficients
Linear coefficients, specified as a length-two numeric vector.
coeffs contains the coefficients of a line defined
y = coeffs(1)*x + coeffs(2).
ax— Target axes
Target axes, specified as an
axes object. If you do not
specify the axes and if the current axes are Cartesian axes, then the
refline function uses the current axes.
hline— One or more reference line objects
One or more reference line objects, returned as a scalar or a vector. These objects are unique identifiers, which you can use to query and modify properties of a specific reference line. For a list of properties, see Line Properties.