Histogram with Probability Curve
A probability curve may be generated for a Gaussian distributed sample.
ScottPlot.Plot myPlot = new();
// Create a histogram from a collection of values
double[] heights = SampleData.MaleHeights();
var hist = ScottPlot.Statistics.Histogram.WithBinCount(100, heights);
// Display the histogram as a bar plot
var barPlot = myPlot.Add.Bars(hist.Bins, hist.GetProbability());
// Customize the style of each bar
foreach (var bar in barPlot.Bars)
{
bar.Size = hist.FirstBinSize;
bar.LineWidth = 0;
bar.FillStyle.AntiAlias = false;
bar.FillColor = Colors.C0.Lighten(.3);
}
// Plot the probability curve on top the histogram
ScottPlot.Statistics.ProbabilityDensity pd = new(heights);
double[] xs = Generate.Range(heights.Min(), heights.Max(), 1);
double sumBins = hist.Bins.Select(x => pd.GetY(x)).Sum();
double[] ys = pd.GetYs(xs, 1.0 / sumBins);
var curve = myPlot.Add.ScatterLine(xs, ys);
curve.LineWidth = 2;
curve.LineColor = Colors.Black;
curve.LinePattern = LinePattern.DenselyDashed;
// Customize plot style
myPlot.Axes.Margins(bottom: 0);
myPlot.YLabel("Probability (%)");
myPlot.XLabel("Height (cm)");
myPlot.SavePng("demo.png", 400, 300);
This recipe is one of many in the Histogram category
