public void ExecuteRecipe(Plot plt) { // generate sample heights are based on https://ourworldindata.org/human-height Random rand = new(0); double[] heights = ScottPlot.DataGen.RandomNormal(rand, pointCount: 1234, mean: 178.4, stdDev: 7.6); // create a histogram var hist = new ScottPlot.Statistics.Histogram(heights, min: 140, max: 220, binSize: 1); // display histogram probabability as a bar plot var bar = plt.AddBar(values: hist.countsFrac, positions: hist.bins); bar.FillColor = ColorTranslator.FromHtml("#9bc3eb"); bar.BorderColor = ColorTranslator.FromHtml("#82add9"); bar.BarWidth = hist.binSize; // display histogram distribution curve as a line plot plt.AddScatterLines( xs: hist.bins, ys: hist.probability, color: Color.Black, lineWidth: 2, lineStyle: LineStyle.Dash); // customize the plot style plt.Title("Adult Male Height"); plt.YAxis.Label("Probability"); plt.XAxis.Label("Height (cm)"); plt.SetAxisLimits(yMin: 0); }
public void ExecuteRecipe(Plot plt) { // create sample data for two datasets Random rand = new Random(0); double[] values1 = DataGen.RandomNormal(rand, pointCount: 1000, mean: 50, stdDev: 20); double[] values2 = DataGen.RandomNormal(rand, pointCount: 1000, mean: 45, stdDev: 25); var hist1 = new ScottPlot.Statistics.Histogram(values1, min: 0, max: 100); var hist2 = new ScottPlot.Statistics.Histogram(values2, min: 0, max: 100); // display datasets as step plots var sp1 = plt.AddScatter(hist1.bins, hist1.cumulativeFrac); sp1.Label = "Sample A"; sp1.StepDisplay = true; sp1.MarkerSize = 0; var sp2 = plt.AddScatter(hist2.bins, hist2.cumulativeFrac); sp2.Label = "Sample B"; sp2.StepDisplay = true; sp2.MarkerSize = 0; // decorate the plot plt.Legend(); plt.SetAxisLimits(yMin: 0, yMax: 1); plt.Grid(lineStyle: LineStyle.Dot); plt.Title("Cumulative Probability Histogram"); plt.XAxis.Label("Probability (fraction)"); plt.YAxis.Label("Value (units)"); }
public void ExecuteRecipe(Plot plt) { Random rand = new Random(0); double[] values = DataGen.RandomNormal(rand, pointCount: 1000, mean: 50, stdDev: 20); var hist = new ScottPlot.Statistics.Histogram(values, min: 0, max: 100); // plot the bins as a bar graph (on the primary Y axis) var bar = plt.AddBar(hist.counts, hist.bins); bar.BarWidth = hist.binSize * 1.2; // oversize to reduce render artifacts bar.BorderLineWidth = 0; bar.YAxisIndex = 0; plt.YAxis.Label("Count (#)"); plt.YAxis.Color(bar.FillColor); // plot the mean curve as a scatter plot (on the secondary Y axis) var sp = plt.AddScatter(hist.bins, hist.countsFracCurve); sp.MarkerSize = 0; sp.LineWidth = 2; sp.YAxisIndex = 1; plt.YAxis2.Label("Fraction"); plt.YAxis2.Color(sp.Color); plt.YAxis2.Ticks(true); // decorate the plot plt.XAxis2.Label("Normal Random Data", bold: true); plt.XAxis.Label("Value (units)"); plt.SetAxisLimits(yMin: 0); plt.Grid(lineStyle: LineStyle.Dot); }
void PlotHistogram() { double[] values = { 0, 20, 25, 28, 39, 59 }; var his = new ScottPlot.Statistics.Histogram(values, min: 0, max: 70); double barWidth = his.binSize * 1.2; plt.PlotBar(his.bins, his.countsFracCurve, barWidth: barWidth, outlineWidth: 0); plt.PlotScatter(his.bins, his.countsFracCurve, markerSize: 0, lineWidth: 2, color: Color.Black); plt.Axis(null, null, 0, null); }
public void ExecuteRecipe(Plot plt) { // male and female heights are based on https://ourworldindata.org/human-height Random rand = new(0); double[] heightsMale = ScottPlot.DataGen.RandomNormal(rand, pointCount: 2345, mean: 178.4, stdDev: 7.6); double[] heightsFemale = ScottPlot.DataGen.RandomNormal(rand, pointCount: 1234, mean: 164.7, stdDev: 7.1); // calculate histograms for male and female datasets var histMale = new ScottPlot.Statistics.Histogram(heightsMale, min: 140, max: 210, binSize: 1); var histFemale = new ScottPlot.Statistics.Histogram(heightsFemale, min: 140, max: 210, binSize: 1); // plot histograms var barMale = plt.AddBar(values: histMale.countsFrac, positions: histMale.bins); barMale.BarWidth = histMale.binSize; barMale.FillColor = Color.FromArgb(50, Color.Blue); barMale.BorderLineWidth = 0; var barFemale = plt.AddBar(values: histFemale.countsFrac, positions: histFemale.bins); barFemale.BarWidth = histFemale.binSize; barFemale.FillColor = Color.FromArgb(50, Color.Red); barFemale.BorderLineWidth = 0; // plot probability function curves plt.AddScatterLines( xs: histMale.bins, ys: histMale.probability, color: Color.FromArgb(150, Color.Blue), lineWidth: 3, label: $"Male (n={heightsMale.Length:N0})"); plt.AddScatterLines( xs: histFemale.bins, ys: histFemale.probability, color: Color.FromArgb(150, Color.Red), lineWidth: 3, label: $"Female (n={heightsFemale.Length:N0})"); // customize styling plt.Title("Human Height by Sex"); plt.YLabel("Probability"); plt.XLabel("Height (cm)"); plt.Legend(location: ScottPlot.Alignment.UpperLeft); plt.SetAxisLimits(yMin: 0); }
public void Render(Plot plt) { Random rand = new Random(0); double[] values = DataGen.RandomNormal(rand, pointCount: 1000, mean: 50, stdDev: 20); var hist = new ScottPlot.Statistics.Histogram(values, min: 0, max: 100); double barWidth = hist.binSize * 1.2; // slightly over-side to reduce anti-alias rendering artifacts plt.PlotBar(hist.bins, hist.countsFrac, barWidth: barWidth, outlineWidth: 0); plt.PlotScatter(hist.bins, hist.countsFracCurve, markerSize: 0, lineWidth: 2, color: Color.Black); plt.Title("Normal Random Data"); plt.YLabel("Frequency (fraction)"); plt.XLabel("Value (units)"); plt.Axis(null, null, 0, null); plt.Grid(lineStyle: LineStyle.Dot); }
public void Render(Plot plt) { // create sample data for two datasets Random rand = new Random(0); double[] values1 = DataGen.RandomNormal(rand, pointCount: 1000, mean: 50, stdDev: 20); double[] values2 = DataGen.RandomNormal(rand, pointCount: 1000, mean: 45, stdDev: 25); var hist1 = new ScottPlot.Statistics.Histogram(values1, min: 0, max: 100); var hist2 = new ScottPlot.Statistics.Histogram(values2, min: 0, max: 100); // display datasets as step plots plt.Title("Cumulative Probability Histogram"); plt.YLabel("Probability (fraction)"); plt.XLabel("Value (units)"); plt.PlotStep(hist1.bins, hist1.cumulativeFrac, lineWidth: 1.5, label: "sample A"); plt.PlotStep(hist2.bins, hist2.cumulativeFrac, lineWidth: 1.5, label: "sample B"); plt.Legend(); plt.Axis(null, null, 0, 1); }
public void ExecuteRecipe(Plot plt) { // generate sample heights are based on https://ourworldindata.org/human-height Random rand = new(0); double[] heights = ScottPlot.DataGen.RandomNormal(rand, pointCount: 1234, mean: 178.4, stdDev: 7.6); // create a histogram var hist = new ScottPlot.Statistics.Histogram(heights, min: 140, max: 220, binSize: 1); // display the histogram counts as a bar plot var bar = plt.AddBar(hist.counts, hist.bins); bar.BarWidth = hist.binSize; // customize the plot style plt.YAxis.Label("Count (#)"); plt.XAxis.Label("Height (cm)"); plt.SetAxisLimits(yMin: 0); }
private void PlotResults(double[] timesMsec) { double mean = timesMsec.Sum() / timesMsec.Length; Array.Sort(timesMsec); double median = timesMsec[(int)(timesMsec.Length / 2)]; var hist = new ScottPlot.Statistics.Histogram(timesMsec); formsPlot2.plt.Clear(); formsPlot2.plt.PlotBar(hist.bins, hist.counts); formsPlot2.plt.PlotVLine(mean, lineWidth: 3, label: "mean"); formsPlot2.plt.PlotVLine(median, lineWidth: 3, label: "median", lineStyle: ScottPlot.LineStyle.Dot); formsPlot2.plt.Legend(); formsPlot2.plt.AxisAuto(); formsPlot2.plt.Axis(y1: 0); formsPlot2.plt.YLabel("Count"); formsPlot2.plt.XLabel("Render Time (ms)"); formsPlot2.plt.Title($"Median: {median} ms"); formsPlot2.Render(); }
private WpfPlot DisplayNormalDistribution(DataColumn column, int columnIndex, int columnsCount) { var wpfPlot = new WpfPlot { Margin = new System.Windows.Thickness(columnIndex * (1800 / columnsCount), 0, 1800 - (columnIndex + 1) * (1800 / columnsCount), 500) }; (var minimum, var maximum) = DistributionProcessor.GetColumnBoundaries(column); var normalDistributionEmpirical = new ScottPlot.Statistics.Histogram(column.Data.ToArray(), binCount: 10, min: minimum, max: maximum); double barWidth = normalDistributionEmpirical.binSize * 1.2; wpfPlot.plt.PlotBar(normalDistributionEmpirical.bins, normalDistributionEmpirical.countsFrac.Select(v => v / normalDistributionEmpirical.binSize).ToArray(), barWidth: barWidth, outlineWidth: 0, fillColor: Color.Gray); wpfPlot.plt.PlotScatter(column.NormalDistribution.Select(nd => nd.x).ToArray(), column.NormalDistribution.Select(nd => nd.y).ToArray(), markerSize: 0, lineWidth: 2); wpfPlot.plt.AxisAutoY(margin: 0); wpfPlot.plt.Axis(x1: minimum); wpfPlot.plt.Ticks(numericFormatStringY: "0.00"); wpfPlot.plt.Title(column.Name); return(wpfPlot); }
public static Mat MakeHistogram(Mat mat) { double[] array; unsafe { var span = new ReadOnlySpan <byte>(mat.Data.ToPointer(), (int)mat.Total() * mat.Channels()); array = new double[span.Length]; for (int i = 0; i < span.Length; i++) { array[i] = span[i]; } } var plt = new Plot(640, 480); var hist = new ScottPlot.Statistics.Histogram(array, min: 0, max: 255, binSize: 1); plt.PlotBar(hist.bins, hist.counts, barWidth: hist.binSize, outlineWidth: 0); plt.Axis(null, null, 0, null); plt.Grid(lineStyle: LineStyle.Dot); var bitmap = plt.GetBitmap(); return(bitmap.ToMat()); }
public void RenderPlot() { plotFrame.plt.Clear(); RefreshTitleAndAxis(false); bool containsDateAxis = false; foreach (PlotParameters curr in ((App)App.Current).GetSeries()) { if (curr.drawSettings.label == "") { curr.drawSettings.label = null;//Prevents it from showing up in the legend } if (curr.drawSettings.dateXAxis) { containsDateAxis = true; object timeUnit; object numTimeUnitsObj; curr.metaData.TryGetValue("timeUnit", out timeUnit); curr.metaData.TryGetValue("numTimeUnits", out numTimeUnitsObj); ScottPlot.Config.DateTimeUnit dateTimeUnit = (ScottPlot.Config.DateTimeUnit)timeUnit; int numTimeUnits = (int)numTimeUnitsObj; dateUnit = dateTimeUnit; dateUnitSpacing = numTimeUnits; if (dateTimeUnit == ScottPlot.Config.DateTimeUnit.Year)//Grid spacing of one year is currently unsupported -_- { plotFrame.plt.Grid(xSpacing: 12, xSpacingDateTimeUnit: ScottPlot.Config.DateTimeUnit.Month); } else { plotFrame.plt.Grid(xSpacing: numTimeUnits, xSpacingDateTimeUnit: dateTimeUnit); } plotFrame.plt.Ticks(dateTimeX: true, xTickRotation: 30, fontSize: 10); // Default fontSize is 12 } switch (curr.drawSettings.type) { case PlotType.scatter: double[] xsScatter = ((double[][])curr.data)[0]; double[] ysScatter = ((double[][])curr.data)[1]; if (logAxis) { ysScatter = ScottPlot.Tools.Log10(ysScatter); } if (!curr.hasErrorData) { plotFrame.plt.PlotScatterHighlight(xsScatter, ysScatter, curr.drawSettings.colour, curr.drawSettings.drawLine ? 1 : 0, label: curr.drawSettings.label, markerShape: curr.drawSettings.markerShape, highlightedColor: curr.drawSettings.colour); } else { double[] errorX = ((double[][])curr.errorData)[0]; double[] errorY = ((double[][])curr.errorData)[1]; plotFrame.plt.PlotScatterHighlight(xsScatter, ysScatter, curr.drawSettings.colour, curr.drawSettings.drawLine ? 1 : 0, label: curr.drawSettings.label, markerShape: curr.drawSettings.markerShape, errorX: errorX, errorY: errorY, highlightedColor: curr.drawSettings.colour); } if (curr.drawSettings.drawLinearRegression) { var model = new ScottPlot.Statistics.LinearRegressionLine(xsScatter, ysScatter); double x1 = xsScatter[0]; double x2 = xsScatter[xsScatter.Length - 1]; double y1 = model.GetValueAt(x1); double y2 = model.GetValueAt(x2); plotFrame.plt.PlotLine(x1, y1, x2, y2, lineWidth: 3, label: $"ŷ = {model.offset:f9} + {model.slope:f9}x"); } break; case PlotType.signal: object sampleRate = 100; curr.metaData.TryGetValue("sampleRate", out sampleRate); object xOffset = 0; curr.metaData.TryGetValue("xOffset", out xOffset); double[] data = (double[])curr.data; if (logAxis) { data = ScottPlot.Tools.Log10(data); } plotFrame.plt.PlotSignalConst(data, (double)sampleRate, (double)xOffset, color: curr.drawSettings.colour, lineWidth: curr.drawSettings.drawLine ? 1 : 0, label: curr.drawSettings.label, markerSize: 0); break; case PlotType.bar: double[] xsBar = ((double[][])curr.data)[0]; double[] ysBar = ((double[][])curr.data)[1]; if (logAxis) { ysBar = ScottPlot.Tools.Log10(ysBar); } if (!curr.hasErrorData) { plotFrame.plt.PlotBar(xsBar, ysBar, fillColor: curr.drawSettings.colour, outlineColor: curr.drawSettings.colour, errorColor: curr.drawSettings.colour, label: curr.drawSettings.label); } else { double[] errorY = ((double[])curr.errorData); plotFrame.plt.PlotBar(xsBar, ysBar, fillColor: curr.drawSettings.colour, outlineColor: curr.drawSettings.colour, errorColor: curr.drawSettings.colour, label: curr.drawSettings.label, errorY: errorY); } break; case PlotType.histogram: ScottPlot.Statistics.Histogram histogram = new ScottPlot.Statistics.Histogram((double[])curr.data); double[] yData = histogram.counts; switch (curr.drawSettings.histogramType) { case HistogramType.fraction | HistogramType.density: yData = histogram.countsFrac; break; case HistogramType.fraction | HistogramType.cumulative: yData = histogram.cumulativeFrac; break; case HistogramType.count | HistogramType.cumulative: yData = histogram.cumulativeCounts; break; } plotFrame.plt.PlotBar(histogram.bins, yData, fillColor: curr.drawSettings.colour, outlineColor: curr.drawSettings.colour, errorColor: curr.drawSettings.colour, label: curr.drawSettings.label); break; case PlotType.horizontal_line: double hLineData = (double)curr.data; plotFrame.plt.PlotHLine(hLineData, color: curr.drawSettings.colour, label: curr.drawSettings.label); break; case PlotType.vertical_line: double vLineData = (double)curr.data; plotFrame.plt.PlotVLine(vLineData, color: curr.drawSettings.colour, label: curr.drawSettings.label); break; case PlotType.horizontal_span: (double hSpanMin, double hSpanMax) = (ValueTuple <double, double>)curr.data; plotFrame.plt.PlotHSpan(hSpanMin, hSpanMax, color: curr.drawSettings.colour, label: curr.drawSettings.label); break; case PlotType.vertical_span: (double vSpanMin, double vSpanMax) = (ValueTuple <double, double>)curr.data; plotFrame.plt.PlotVSpan(vSpanMin, vSpanMax, color: curr.drawSettings.colour, label: curr.drawSettings.label); break; case PlotType.box_whisker: var plotObj = plotFrame.plt.PlotPopulations((ScottPlot.Statistics.Population)curr.data, label: curr.drawSettings.label); plotObj.boxStyle = PlottablePopulations.BoxStyle.BoxMedianQuartileOutlier; plotObj.displayDistributionCurve = false; break; case PlotType.function: var f = (Func <double, double?>)curr.data; plotFrame.plt.PlotFunction(f, label: curr.drawSettings.label, markerShape: MarkerShape.none, lineStyle: LineStyle.Dash); break; case PlotType.bar_grouped: var plotData = ((double[][])curr.data); string[] groupLabels = (string[])curr.metaData.GetValueOrDefault("group_names"); string[] seriesLabels = (string[])curr.metaData.GetValueOrDefault("series_names"); if (!curr.hasErrorData) { plotFrame.plt.PlotBarGroups(groupLabels, seriesLabels, plotData); } else { plotFrame.plt.PlotBarGroups(groupLabels, seriesLabels, plotData, (double[][])curr.errorData); } break; } } isDateAxis = containsDateAxis; plotFrame.plt.Legend(); (double highlightX, double highlightY) = (plotFrame.plt.Axis()[0], plotFrame.plt.Axis()[3]); foreach (var curr in rawPlottables) { if (curr != snappedCoordinates || showCoordinates) { plotFrame.plt.Add(curr);//Got axed when we cleared everything } } RenderFrame(); }
private void PlotHistogram() { double?min = (double)nudMin.Value; double?max = (double)nudMax.Value; if (cbMinAuto.Checked) { min = null; } if (cbMaxAuto.Checked) { max = null; } double?binSize = (double)nudBinSize.Value; double?binCount = (double)nudBinCount.Value; if (cbBinSizeAuto.Checked) { binSize = null; } if (cbBinCountAuto.Checked) { binCount = null; } // ignore binCount if both binSize and binCount are given if ((binSize != null) && (binCount != null)) { binCount = null; } bool ignoreOutOfBounds = cbIgnoreOutOfBounds.Checked; var hist = new ScottPlot.Statistics.Histogram(values, min, max, binSize, binCount, ignoreOutOfBounds); binSize = hist.bins[1] - hist.bins[0]; if (nudMin.Enabled == false) { nudMin.Value = (decimal)hist.bins[0]; } if (nudMax.Enabled == false) { nudMax.Value = (decimal)(hist.bins[hist.bins.Length - 1] + binSize); } if (nudBinSize.Enabled == false) { nudBinSize.Value = (decimal)binSize; } if (nudBinCount.Enabled == false) { nudBinCount.Value = (decimal)hist.bins.Length; } lbBins.Items.Clear(); foreach (double bin in hist.bins) { lbBins.Items.Add(bin.ToString()); } if (cbCount.Checked) { PlotHistogramCount(hist.bins, hist.counts); } else if (cbNorm.Checked) { PlotHistogramFrac(hist.bins, hist.countsFrac); } else if (cbCph.Checked) { PlotHistogramCumulative(hist.bins, hist.cumulativeFrac); } }