lsline — Add least-squares fit lines to scatter-style plots.

lsline appends least-squares fit lines to eligible scatter-style series in the current or specified axes. It matches MATLAB's compatibility surface for adding fit lines to scatter plots and marker-only line plots, returning the created line handle or handles when requested.

Syntax

lsline()
lsline(ax)
h = lsline()
h = lsline(ax)

Inputs

NameTypeRequiredDefaultDescription
axAxesHandleYesTarget axes handle.

Returns

NameTypeDescription
hNumericArrayLine graphics handle or vector of line handles.

Errors

IdentifierWhenMessage
RunMat:lsline:InvalidArgumentThe optional axes handle is malformed or extra arguments are supplied.lsline: invalid argument
RunMat:lsline:InternalRunMat cannot construct or register the least-squares line.lsline: internal error

How lsline works

  • lsline() scans the current axes and adds one least-squares line for each visible eligible scatter-style plot.
  • lsline(ax) targets the specified axes without changing other axes.
  • Eligible plots include scatter plots and marker-only line plots. Ordinary line plots with a visible connecting line are ignored.
  • Non-finite X/Y pairs are skipped when computing the fit. A vertical or constant-X eligible series produces a horizontal least-squares line at the mean Y value.
  • If no eligible plots are present, RunMat returns an empty 0x0 handle array.
  • The returned line handle is compatible with get and set for line properties such as XData, YData, Color, LineStyle, LineWidth, and DisplayName.

Examples

Add a least-squares line to a scatter plot

x = [1 2 3 4];
y = [1.2 1.9 3.1 3.8];
scatter(x, y);
h = lsline;

Target a specific axes

ax = subplot(1,2,2);
scatter(ax, [1 2 3], [2 4 6]);
h = lsline(ax);

Inspect the generated line data

scatter([1 2 3], [2 4 6]);
h = lsline;
get(h, 'YData')

Expected output:

ans =
     2     6

Using lsline with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how lsline changes the result.

Run a small lsline example, explain the result, then change one input and compare the output.

FAQ

How is lsline related to refline?

lsline only computes least-squares fit lines for eligible scatter-style plots. refline can do the same when called without coefficients, but it also accepts explicit slope/intercept coefficients for drawing arbitrary reference lines.

Does lsline fit every plot in the axes?

No. MATLAB-style lsline is intended for scatter-style data. RunMat fits scatter plots and marker-only line plots and ignores connected line plots, surfaces, bars, and other graphics objects.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how lsline is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.