线性回归
使线条适合 X/Y 数据点的集合。
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 = WpfPlot1.Plot.Add.Scatter(xs, ys);
sp.LineWidth = 0;
sp.MarkerSize = 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 = WpfPlot1.Plot.Add.Line(pt1, pt2);
line.MarkerSize = 0;
line.LineWidth = 2;
line.LinePattern = LinePattern.Dashed;
// note the formula at the top of the plot
WpfPlot1.Plot.Title(reg.FormulaWithRSquared);
WpfPlot1.Refresh();