Merged
Conversation
Please review. Adds LinearRegressionLine to ScottPlot5. So it can be used with code similar to the following:
```
// Linear regression line
double firstDate = dates[0];
double lastDate = dates[dates.Length - 1];
var regressionLine = new ScottPlot.Statistics.LinearRegressionLine(dates, chartValues);
AddLine(_plot.Plot, regressionLine.slope, regressionLine.offset, (firstDate, lastDate), lineWidth: 0.5F, color: XkcdColors.LightGreyBlue, label: "Trend");
}
private void AddLine(Plot plot, double slope, double offset, (double x1, double x2) xLimits, string label = "", Color? color = null, float lineWidth = 1)
{
double y1 = xLimits.x1 * slope + offset;
double y2 = xLimits.x2 * slope + offset;
var scatter = plot.Add.Scatter(new double[] { xLimits.x1, xLimits.x2 }, new double[] { y1, y2 }, color);
if (!string.IsNullOrWhiteSpace(label))
scatter.Label = label;
}
```
Closed
Member
|
Thanks for this PR @anewton! I'm extending it a bit, adding tests, and creating a cookbook demo. It's going to work something like this: double[] xs = new double[] { 1, 2, 3, 4, 5, 6, 7 };
double[] ys = new double[] { 2, 2, 3, 3, 3.8, 4.2, 4 };
// plot original data as a scatter plot
var sp = myPlot.Add.Scatter(xs, ys);
sp.LineStyle = LineStyle.None;
sp.MarkerStyle.Size = 10;
// calculate the regression line
ScottPlot.Statistics.LinearRegression reg = new(xs, ys);
// plot the regression line
Coordinates pt1 = new(xs.First(), reg.GetValue(xs.First()));
Coordinates pt2 = new(xs.Last(), reg.GetValue(xs.Last()));
var line = myPlot.Add.Line(pt1, pt2);
line.MarkerStyle = MarkerStyle.None;
line.LineStyle.Pattern = LinePattern.Dash;
line.LineStyle.Width = 2;
// note the formula at the top of the plot
myPlot.Title($"y = {reg.Slope:0.###}x + {reg.Offset:0.###} (r²={reg.Rsquared:0.###})"); |
Contributor
Author
|
Thanks for cleaning up the PR!! I know it was a bit messy and did not probably follow your repo PR requirements. Was going to come back to it and resubmit. But it looks like you have it and the changes look awesome. Very much look forward to using this in my Avalonia desktop apps. Thanks @swharden! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Please review. Adds LinearRegressionLine to ScottPlot5. So it can be used with code similar to the following:
Purpose: