public static Plot CreateAdvancedStatisticsPlot() { var plt = new ScottPlot.Plot(600, 400); // create some sample data to represent test scores Random rand = new Random(0); double[] scores = DataGen.RandomNormal(rand, 250, 85, 5); // create a Population object from the data var pop = new ScottPlot.Statistics.Population(scores); // display the original values scattered vertically double[] ys = DataGen.RandomNormal(rand, pop.values.Length, stdDev: .15); plt.PlotScatter(pop.values, ys, markerSize: 10, markerShape: MarkerShape.openCircle, lineWidth: 0); // display the bell curve for this distribution double[] curveXs = DataGen.Range(pop.minus3stDev, pop.plus3stDev, .1); double[] curveYs = pop.GetDistribution(curveXs); plt.PlotScatter(curveXs, curveYs, markerSize: 0, lineWidth: 2); // improve the style of the plot plt.Title($"Test Scores (mean: {pop.mean:0.00} +/- {pop.stDev:0.00}, n={pop.n})"); plt.XLabel("Score"); plt.Grid(lineStyle: LineStyle.Dot); return(plt); //plt.SaveFig("Advanced_Statistics_Population.png"); }
public void Test_Population_CurveSideways() { Random rand = new Random(0); var ages = new ScottPlot.Statistics.Population(rand, 44, 78, 2); double[] curveXs = DataGen.Range(ages.minus3stDev, ages.plus3stDev, .1); double[] curveYs = ages.GetDistribution(curveXs); var plt = new ScottPlot.Plot(400, 300); plt.PlotScatter(DataGen.Random(rand, ages.values.Length), ages.values, markerSize: 10, markerShape: MarkerShape.openCircle, lineWidth: 0); plt.PlotScatter(curveYs, curveXs, markerSize: 0); plt.Grid(lineStyle: LineStyle.Dot); TestTools.SaveFig(plt); }
public void Render(Plot plt) { // create some sample data to represent test scores Random rand = new Random(0); double[] scores = DataGen.RandomNormal(rand, 35, 85, 5); // create a Population object and plot it var pop = new ScottPlot.Statistics.Population(scores); plt.PlotPopulations(pop, "scores"); // improve the style of the plot plt.Title($"Test Scores (mean: {pop.mean:0.00} +/- {pop.stDev:0.00}, n={pop.n})"); plt.YLabel("Score"); plt.Ticks(displayTicksX: false); plt.Legend(); plt.Grid(lineStyle: LineStyle.Dot, enableVertical: false); }
public void GenerateMultipleBoxAndWhiskers(List <List <ResultModel> > allResults, List <int> xValues, string plotTitle, string plotName = "results") { var plt = new ScottPlot.Plot(600, 400); var poulations = new ScottPlot.Statistics.Population[allResults.Count]; var p = 0; foreach (var results in allResults) { var ys = new double[results.Count]; var i = 0; foreach (var r in results) { ys[i] = r.Fitness; i++; } var pop = new ScottPlot.Statistics.Population(ys); poulations[p] = pop; p++; } var popSeries = new ScottPlot.Statistics.PopulationSeries(poulations); plt.PlotPopulations(popSeries, "scores"); // improve the style of the plot plt.Title(plotTitle); plt.YLabel("Solution energy"); var ticks = new string[xValues.Count]; var j = 0; foreach (var iterations in xValues) { ticks[j] = iterations.ToString(); j++; } plt.XTicks(ticks); plt.Legend(); plt.Grid(lineStyle: LineStyle.Dot, enableVertical: false); plt.SaveFig($"{plotName}.png"); }
public void GenerateSingleBoxAndWhiskers(List <ResultModel> results, string plotName = "results") { var plt = new Plot(600, 400); var ys = new double[results.Count]; var i = 0; foreach (var r in results) { ys[i] = r.Fitness; i++; } plt.Title("Solution energy"); plt.YLabel("Solution energy"); var pop = new ScottPlot.Statistics.Population(ys); plt.PlotPopulations(pop, "scores"); plt.SaveFig($"{plotName}.png"); }
public void Test_scatter_10kPoints() { double[] pointCounts = { 10, 100, 1000, 10000 }; const int REPS = 10; double[] speeds = new double[pointCounts.Length]; for (int i = 0; i < pointCounts.Length; i++) { int pointCount = (int)pointCounts[i]; var plt = new ScottPlot.Plot(); Random rand = new Random(0); double[] xs = DataGen.Random(rand, pointCount); double[] ys = DataGen.RandomWalk(rand, pointCount); plt.AddScatter(xs, ys); plt.Render(lowQuality: true); List <double> times = new List <double>(); for (int j = 0; j < REPS; j++) { plt.Render(lowQuality: true); times.Add(plt.GetSettings(false).BenchmarkMessage.MSec); } var stats = new ScottPlot.Statistics.Population(times.ToArray()); speeds[i] = stats.mean; Console.WriteLine($"Rendered {pointCount} points in {stats.mean} ms"); } var plt2 = new ScottPlot.Plot(400, 300); plt2.Title("Scatter Plot Benchmark"); plt2.YLabel("Time (ms)"); plt2.XLabel("Number of Points"); plt2.AddScatter(pointCounts, speeds); TestTools.SaveFig(plt2); }
public PlotParameters AddSeriesFromString(string dataString, DrawSettings drawSettings, Dictionary <string, object> metadata) { object data = new object(); string[] raw = dataString.Split(new char[] { ',', '\n' }); if (!drawSettings.dateXAxis) { List <double> serialData = raw.Where(m => double.TryParse(m, out _)).Select(m => double.Parse(m)).ToList(); if (drawSettings.type == PlotType.scatter || drawSettings.type == PlotType.bar) { double[] xs = new double[serialData.Count / 2]; double[] ys = new double[serialData.Count / 2]; for (int i = 0; i < serialData.Count; i++) { int row = i / 2; int col = i % 2; if (col == 0) { xs[row] = serialData[i]; } else { ys[row] = serialData[i]; } data = new double[][] { xs, ys }; } } else if (drawSettings.type == PlotType.signal || drawSettings.type == PlotType.histogram) { data = serialData.ToArray(); } else if (drawSettings.type == PlotType.box_whisker) { ScottPlot.Statistics.Population dataPopulation = new ScottPlot.Statistics.Population(serialData.ToArray()); data = dataPopulation; } if (drawSettings.type == PlotType.scatter && drawSettings.polarCoordinates) { (((double[][])data)[0], ((double[][])data)[1]) = ScottPlot.Tools.ConvertPolarCoordinates(((double[][])data)[0], ((double[][])data)[1]); } } else { if (drawSettings.type == PlotType.scatter || drawSettings.type == PlotType.bar) { List <DateTime> dataX = new List <DateTime>(); List <double> dataY = new List <double>(); int successfullyParsed = 0; for (int i = 0; i < raw.Length; i++) { if (successfullyParsed % 2 == 0) { DateTime temp; if (DateTime.TryParse(raw[i], out temp)) { dataX.Add(temp); successfullyParsed++; } } else { double temp; if (double.TryParse(raw[i], out temp)) { dataY.Add(temp); successfullyParsed++; } } } long totalDistanceTicks = 0; for (int i = 0; i < dataX.Count; i++) { if (i == dataX.Count - 1) { break; } totalDistanceTicks += Math.Abs(dataX[i].Ticks - dataX[i + 1].Ticks); } long averageTickDistance = totalDistanceTicks / (dataX.Count - dataX.Count % 2); //The fact that this is rounded doesn't matter, bc ticks are so obsenely small long averageSecondsDistance = averageTickDistance / 10_000_000; metadata["numTimeUnits"] = 1; if (averageSecondsDistance > 0.75 * 86400 * 365) { metadata["timeUnit"] = ScottPlot.Config.DateTimeUnit.Year; } else if (averageSecondsDistance > 0.75 * 86400 * 30) { metadata["timeUnit"] = ScottPlot.Config.DateTimeUnit.Month; } else if (averageSecondsDistance > 0.75 * 86400) { metadata["timeUnit"] = ScottPlot.Config.DateTimeUnit.Day; } else if (averageSecondsDistance > 0.75 * 3600) { metadata["timeUnit"] = ScottPlot.Config.DateTimeUnit.Hour; } else if (averageSecondsDistance > 0.75 * 60) { metadata["timeUnit"] = ScottPlot.Config.DateTimeUnit.Minute; } else { metadata["timeUnit"] = ScottPlot.Config.DateTimeUnit.Second; } metadata["startDate"] = dataX[0]; metadata["numTimeUnits"] = (dataX.Count / 30) + 1; double[] dataXDouble = dataX.Select((dt, i) => dt.ToOADate()).ToArray(); data = new double[][] { dataXDouble, dataY.ToArray() }; } } PlotParameters plotParams = new PlotParameters { data = data, drawSettings = drawSettings, metaData = metadata }; series.Add(plotParams); ((MainWindow)this.MainWindow).RenderPlot(); return(plotParams); }
private void formsPlot1_Load(object sender, EventArgs e) { // this is the plotting place - all in the Load // string tname1 = "WXROVER"; // string tname2 = "WXIN"; // int pointcount = 5000; // double[,] tempData; int cnt = 0; // double[] t1 = new double[Globals.pointcount]; // double[] t2 = new double[Globals.pointcount]; // a couple of local lists List <double> orgs = new List <double> { }; List <double> chks = new List <double> { }; classWXstnChk WXchk = new classWXstnChk(); //make an object WXck WXchk.cnxstring("192.168.1.15", "DAWES_SQL2008", "WeatherStation", "WeatherStation", "Esp32a.b."); //a constructor - no arguments //go get the data Globals.tempData = WXchk.getTemperatureData(Globals.pointcount, Globals.tname1, Globals.tname2); //this gets the data as an XY pair //there will be a lot of duplcates in this array. Lets remove the dupes - done in the SQL //for scotterplot to work, these need to be in two separate Arrays, so we seperate them out in //this loop and we might as well get rid of the empty slots //maybe this would also work // double [] tab1 = new double[tempData.Length]; for (int i = 0; i < pointcount - 1; i++) { if (Globals.tempData[i, 0] > 0) { t1[i] = tempData[i, 0]; t2[i] = tempData[i, 1]; t1qq[i] = tempData[i, 0]; t2qq[i] = tempData[i, 1]; //might as well put the data in the grid dgXY.Rows.Add(); try { dgXY.Rows[i].Cells["dgXYrecord"].Value = i.ToString(); //puts the record no on the dgXY dgXY.Rows[i].Cells["dgXYx"].Value = t1[i]; dgXY.Rows[i].Cells["dgXYy"].Value = t2[i]; } catch { } cnt += 1; } }//end of for loop // we have two arrays lets sort them to form a QQ plot Array.Sort(t1qq); Array.Sort(t2qq); // the arrays are declared a certain size and are now full of 0's //this will reduce them down to just data. // if there is no data in the arrays then cnt will = 0 if (cnt == 0) { MessageBox.Show("No Data in your selection.", "No data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } Array.Resize(ref t1, cnt); Array.Resize(ref t2, cnt); cntG = cnt; // int aa = cnt; int ab = t2.GetLength(0); // create a Population object from the data var popt1 = new ScottPlot.Statistics.Population(t1); var popt2 = new ScottPlot.Statistics.Population(t2); // now lets fill the stats box lblT1.Text = tname1; lblT2.Text = tname2; lblMeanT1.Text = Math.Round(popt1.mean, 2).ToString(); lblMeanT2.Text = Math.Round(popt2.mean, 2).ToString(); lblMedT1.Text = Math.Round(popt1.median, 2).ToString(); lblMedT2.Text = Math.Round(popt2.median, 2).ToString(); lblQ3T1.Text = popt1.Q3.ToString(); lblq3table2.Text = popt1.Q3.ToString(); lblQ1t1.Text = popt1.Q1.ToString(); lblQ1T2.Text = popt2.Q1.ToString(); lblstderrT1.Text = Math.Round(popt1.stdErr, 2).ToString(); lblstderrT2.Text = Math.Round(popt2.stdErr, 2).ToString(); lblstdevT1.Text = Math.Round(popt1.stDev, 2).ToString(); lblstdevT2.Text = Math.Round(popt2.stDev, 2).ToString(); lblIQRt1.Text = Math.Round(popt1.IQR, 2).ToString(); lblIQRt2.Text = Math.Round(popt2.IQR, 2).ToString(); lblNtn1.Text = cnt.ToString(); //opt1.aa.ToString(); // lblNtn2.Text = ab.ToString(); // popt2.count.ToString(); //this gives us the regression line and the correlation coeficient var model = new ScottPlot.Statistics.LinearRegressionLine(t1, t2); string line = $"Y={model.slope:0.0000}X + {model.offset:0.0}"; lblLine.Text = line; lblCorrel.Text = $"R² = {model.rSquared:0.0000}"; formsPlot1.plt.Title($"ScarpWeather Temperature XY Plot"); formsPlot1.plt.XLabel(tname1); formsPlot1.plt.YLabel(tname2); //formsPlot1.plt.AxisAuto(horizontalMargin: 0, verticalMargin: 0.5); // xmin,xmax,ymin,ymax int axisMin, axisMax; axisMin = (int)popt1.min - 2; //get some data from the arrays //axisMin = 0; axisMax = (int)popt1.max + 2; formsPlot1.plt.Axis(axisMin, axisMax, axisMin, axisMax); //defines the min and max of the chart // formsPlot1.plt.Axis(15,40,15,40); formsPlot1.plt.Legend(); // got ot have this be we toggle it with hte context menu //this draws the scatter plot XYplotPoints = formsPlot1.plt.PlotScatter(t1, t2, Color.DarkCyan, lineWidth: 0, markerSize: 5, label: "XYData"); //draw the QQ plot QQplotPoints = formsPlot1.plt.PlotScatter(t1qq, t2qq, Color.DarkGreen, lineWidth: 0, markerSize: 5, label: "QQData"); QQplotPoints.visible = false; // draws the regresion line //if (plotRegression) regLine = formsPlot1.plt.PlotLine(model.slope, model.offset, (axisMin, axisMax), lineWidth: 2, label: "Regression", color: Color.BlueViolet); //plot a 45 degree line = X=Y XequalsYLine = formsPlot1.plt.PlotLine(1.0, 0.0, (0, 100), Color.Goldenrod, lineWidth: 2, label: "X=Y", lineStyle: ScottPlot.LineStyle.Dot); //uses the equation of the line to plot //the 10% error lines errorLineA = formsPlot1.plt.PlotLine(0, 0, 100.0 - 0.1 * 100, 100, color: Color.OrangeRed, lineWidth: 2, label: "10%error", lineStyle: ScottPlot.LineStyle.Dash); errorLineB = formsPlot1.plt.PlotLine(0, 0, 100, 100.0 - 0.1 * 100, color: Color.OrangeRed, lineWidth: 2, lineStyle: ScottPlot.LineStyle.Dash); formsPlot1.Render();//draw the chart on the PC screen }//end function