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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
ax | AxesHandle | Yes | — | Target axes handle. |
Returns
| Name | Type | Description |
|---|---|---|
h | NumericArray | Line graphics handle or vector of line handles. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:lsline:InvalidArgument | The optional axes handle is malformed or extra arguments are supplied. | lsline: invalid argument |
RunMat:lsline:Internal | RunMat 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
0x0handle array. - The returned line handle is compatible with
getandsetfor line properties such asXData,YData,Color,LineStyle,LineWidth, andDisplayName.
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 6Using 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.
Related Stats functions
Summary
binocdf · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · kstest · kurtosis · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pdf · prctile · quantile · refline · rmse · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest2 · wblinv
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Random
binornd · bootstrp · datasample · dividerand · exprnd · gamrnd · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Hist
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how lsline is executed, line by line, in Rust.
- View the source for lsline in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.