# NMath User's Guide

12.1 Creating Histograms (.NET, C#, CSharp, VB, Visual Basic, F#)

The Histogram class provides various methods for defining the bins into which input data will be sorted. For example, you can create a histogram with a specified number of equal-sized bins spanning specified maximum and minimum values. Thus, this code creates a histogram with 10 equal-sized bins spanning 0.0 to 100.0:

Code Example – C# histogram

```var hist = new Histogram( 10, 0.0, 100.0 );
```

Code Example – VB histogram

```Dim Hist As New Histogram(10, 0.0, 100.0)
```

The first n-1 bins are closed with respect to the lower bound, but open with respect to the upper bound. For instance, in the histogram created above, the first bin includes 0.0 but excludes 10.0, the second bin includes 10.0 but excludes 20.0, and so forth. The final bin is closed with respect to both upper and lower bounds. Thus, in the code above, the last bin includes both 90.0 and 100.0.

If you do not wish to create equal-sized bins, you can create a Histogram from a vector of bin boundaries. Bin boundaries must be strictly monotonically increasing; that is, binBoundares[i] must be strictly less than binBoundaries[i+1] for each i. For example, this constructs a histogram with 3 unequal-sized bins spanning 0.0 to 100.0:

Code Example – C# histogram

```var v = new DoubleVector( "0.0 25.0 75.0 100.0" );
var hist = new Histogram( v );
```

Code Example – VB histogram

```Dim V As New DoubleVector("0.0 25.0 75.0 100.0")
Dim Hist As New Histogram(V)
```

Again, the first n-1 bins are closed with respect to the lower bound, but open with respect to the upper bound. The final bin is closed with respect to both upper and lower bounds.

Finally, for complete control, you can create a Histogram from an array of Interval objects. An Interval represents a numeric interval with inclusive or exclusive lower and upper bounds. The Interval constructor accepts a lower and upper bound, plus a value from the Interval.Type enumeration indicated whether the interval is open or closed with respect to each boundary. Thus:

Code Example – C# histogram

```// (0,10)
var i1 = new Interval(  0, 10, Interval.Type.OpenOpen );

// [0,10)
var i1 = new Interval(  0, 10, Interval.Type.ClosedOpen );

// (0,10]
var i1 = new Interval(  0, 10, Interval.Type.OpenClosed );

// [0,10]
var i1 = new Interval(  0, 10, Interval.Type.ClosedClosed );
```

Code Example – VB histogram

```' (0,10)
Dim I1 As New Interval(0, 10, Interval.Type.OpenOpen)

' [0,10)
Dim I1 As New Interval(0, 10, Interval.Type.ClosedOpen)

' (0,10]
Dim I1 As New Interval(0, 10, Interval.Type.OpenClosed)

' [0,10]
Dim I1 As New Interval(0, 10, Interval.Type.ClosedClosed)
```

A Histogram can be created from an array of Interval objects. The intervals must be continuous and non-overlapping.

Top

Top