直方图快速入门
可以从值集合创建直方图。
// Create a histogram from a collection of values
double[] heights = SampleData.MaleHeights();
var hist = ScottPlot.Statistics.Histogram.WithBinCount(10, heights);
// Display the histogram as a bar plot
var barPlot = WpfPlot1.Plot.Add.Bars(hist.Bins, hist.Counts);
// Size each bar slightly less than the width of a bin
foreach (var bar in barPlot.Bars)
{
bar.Size = hist.FirstBinSize * .8;
}
// Customize plot style
WpfPlot1.Plot.Axes.Margins(bottom: 0);
WpfPlot1.Plot.YLabel("Number of People");
WpfPlot1.Plot.XLabel("Height (cm)");
WpfPlot1.Refresh();
具有固定大小分箱的直方图
可以使用手动定义的 bin 大小创建直方图。
// Create a histogram from a collection of values
double[] heights = SampleData.MaleHeights();
var hist = ScottPlot.Statistics.Histogram.WithBinSize(2, heights);
// Display the histogram as a bar plot
var barPlot = WpfPlot1.Plot.Add.Bars(hist.Bins, hist.Counts);
// Size each bar slightly less than the width of a bin
foreach (var bar in barPlot.Bars)
{
bar.Size = hist.FirstBinSize * .8;
}
// Customize plot style
WpfPlot1.Plot.Axes.Margins(bottom: 0);
WpfPlot1.Plot.YLabel("Number of People");
WpfPlot1.Plot.XLabel("Height (cm)");
WpfPlot1.Refresh();
填充直方图
填充直方图(条形之间没有可见间隙的直方图)可以通过将条形宽度设置为分箱大小来实现。但是,消除锯齿伪影可能会导致小节之间出现白线。为每个条形禁用抗锯齿以改善此类绘图的外观。
// Create a histogram from a collection of values
double[] heights = SampleData.MaleHeights();
var hist = ScottPlot.Statistics.Histogram.WithBinSize(1, heights);
// Display the histogram as a bar plot
var barPlot = WpfPlot1.Plot.Add.Bars(hist.Bins, hist.Counts);
// Customize the style of each bar
foreach (var bar in barPlot.Bars)
{
bar.Size = hist.FirstBinSize;
bar.LineWidth = 0;
bar.FillStyle.AntiAlias = false;
}
// Customize plot style
WpfPlot1.Plot.Axes.Margins(bottom: 0);
WpfPlot1.Plot.YLabel("Number of People");
WpfPlot1.Plot.XLabel("Height (cm)");
WpfPlot1.Refresh();
直方图条
已创建辅助方法和绘图类型,以简化创建显示直方图计数的条形图的过程。请注意,直方图的更新可能会实时显示,并且绘图将自动更新以显示最新数据。
// create an empty histogram and display it as a bar plot
var hist = ScottPlot.Statistics.Histogram.WithBinCount(count: 20, minValue: 140, maxValue: 220);
var histPlot = WpfPlot1.Plot.Add.Histogram(hist);
histPlot.BarWidthFraction = 0.8;
// histogram counts are updated automatically as new data is added
double[] newData = SampleData.MaleHeights();
hist.AddRange(newData);
WpfPlot1.Refresh();
概率直方图
直方图可以显示为落入 bin 中的每个值的概率
// Create a histogram from a collection of values
double[] heights = SampleData.MaleHeights();
var hist = ScottPlot.Statistics.Histogram.WithBinCount(10, heights);
// Display the histogram as a bar plot
var barPlot = WpfPlot1.Plot.Add.Bars(hist.Bins, hist.GetProbability(100));
// Customize the style of each bar
foreach (var bar in barPlot.Bars)
{
bar.Size = hist.FirstBinSize * 0.8;
}
// Customize plot style
WpfPlot1.Plot.Axes.Margins(bottom: 0);
WpfPlot1.Plot.YLabel("Probability (%)");
WpfPlot1.Plot.XLabel("Height (cm)");
WpfPlot1.Refresh();
带概率曲线的直方图
可以为高斯分布样本生成概率曲线。
// 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 = WpfPlot1.Plot.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 = WpfPlot1.Plot.Add.ScatterLine(xs, ys);
curve.LineWidth = 2;
curve.LineColor = Colors.Black;
curve.LinePattern = LinePattern.DenselyDashed;
// Customize plot style
WpfPlot1.Plot.Axes.Margins(bottom: 0);
WpfPlot1.Plot.YLabel("Probability (%)");
WpfPlot1.Plot.XLabel("Height (cm)");
WpfPlot1.Refresh();
具有第二轴概率的直方图
可以将概率曲线放置在辅助轴上,以允许将计数与具有百分比单位的概率一起显示
// 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 = WpfPlot1.Plot.Add.Bars(hist.Bins, hist.Counts);
// Customize the style of each bar
foreach (var bar in barPlot.Bars)
{
bar.Size = hist.FirstBinSize;
bar.LineWidth = 0;
bar.FillStyle.AntiAlias = false;
}
// Add a probability curve to a secondary axis
ScottPlot.Statistics.ProbabilityDensity pd = new(heights);
double[] xs = Generate.Range(heights.Min(), heights.Max(), 1);
double[] ys = pd.GetYs(xs, 100);
var curve = WpfPlot1.Plot.Add.ScatterLine(xs, ys);
curve.Axes.YAxis = WpfPlot1.Plot.Axes.Right;
curve.LineWidth = 2;
curve.LineColor = Colors.Black;
curve.LinePattern = LinePattern.DenselyDashed;
// Customize plot style
WpfPlot1.Plot.Axes.Margins(bottom: 0);
WpfPlot1.Plot.YLabel("Number of People");
WpfPlot1.Plot.XLabel("Height (cm)");
WpfPlot1.Plot.Axes.Right.Label.Text = "Probability (%)";
WpfPlot1.Refresh();
多个直方图
演示如何使用半透明条形显示重叠数据集中的直方图
// Create a histogram from a collection of values
double[][] heightsByGroup = { SampleData.MaleHeights(), SampleData.FemaleHeights() };
string[] groupNames = { "Male", "Female" };
Color[] groupColors = { Colors.Blue, Colors.Red };
for (int i = 0; i < 2; i++)
{
double[] heights = heightsByGroup[i];
var hist = ScottPlot.Statistics.Histogram.WithBinSize(1, heights);
// Display the histogram as a bar plot
var barPlot = WpfPlot1.Plot.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 = groupColors[i].WithAlpha(.2);
}
// Plot the probability curve on top the histogram
ScottPlot.Statistics.ProbabilityDensity pd = new(heights);
double[] xs = Generate.Range(heights.Min(), heights.Max(), 1);
double scale = 1.0 / hist.Bins.Select(x => pd.GetY(x)).Sum();
double[] ys = pd.GetYs(xs, scale);
var curve = WpfPlot1.Plot.Add.ScatterLine(xs, ys);
curve.LineWidth = 2;
curve.LineColor = groupColors[i];
curve.LinePattern = LinePattern.DenselyDashed;
curve.LegendText = groupNames[i];
}
// Customize plot style
WpfPlot1.Plot.Legend.Alignment = Alignment.UpperRight;
WpfPlot1.Plot.Axes.Margins(bottom: 0);
WpfPlot1.Plot.YLabel("Probability (%)");
WpfPlot1.Plot.XLabel("Height (cm)");
WpfPlot1.Plot.HideGrid();
WpfPlot1.Refresh();
累积概率直方图
累积概率直方图表示每个 bin 的概率或相对频率的累积总和,提供概率分布的运行总计。它对于评估和比较多个总体的分布特别有用。
安慰WinForms 公司WPF其他
// Create a histogram from a collection of values
double[][] heightsByGroup = { SampleData.MaleHeights(100), SampleData.FemaleHeights(100) };
string[] groupNames = { "Male", "Female" };
Color[] groupColors = { Colors.Blue, Colors.Red };
for (int i = 0; i < 2; i++)
{
var hist = ScottPlot.Statistics.Histogram.WithBinSize(1, firstBin: 140, lastBin: 200);
hist.AddRange(heightsByGroup[i]);
var curve = WpfPlot1.Plot.Add.ScatterLine(hist.Bins, hist.GetCumulativeProbability(100));
curve.LineWidth = 1.5f;
curve.LineColor = groupColors[i];
curve.LegendText = groupNames[i];
curve.ConnectStyle = ConnectStyle.StepVertical;
}
// Customize plot style
WpfPlot1.Plot.Legend.Alignment = Alignment.LowerRight;
WpfPlot1.Plot.YLabel("Cumulative Probability (%)");
WpfPlot1.Plot.XLabel("Height (cm)");
WpfPlot1.Refresh();