Functions > Statistics > Graphical Statistics > Example: Histograms
Example: Histograms
Create histograms to present data coming from an experiment designed to measure the speed of light.
Histograms are created by specifying an intervals vector or an integer as the first argument to the hist and histogram functions.
The following two vectors contain the data recorded in m/s or km/hr.
 V1 2.9985 10 8 2.9974 10 8 2.9990 10 8 3.0007 10 8 2.9993 10 8 2.9984 10 8 2.9985 10 8 2.9984 10 8 2.9984 10 8 2.9984 10 8
m s
V2 1.07957 10 9 1.07968 10 9 1.07946 10 9 1.07953 10 9 1.07942 10 9 1.07946 10 9 1.07982 10 9 1.07993 10 9 1.07993 10 9 1.07957 10 9
km hr
Specifying an Intervals Vector
1. Use the min function to create a set of bins for V1 so that the V1 and V2 histograms can be compared over the same intervals.
2. Use the histogram function to create the two histograms.
The histogram function accepts a vector containing units.
3. Use the hist function to count the number of elements of V1 and V2 contained in each bin.
 R11 hist int1 V1 R12 hist int1 V2
4. Define the x-axis as column 0 of H11, normalized by 108 to make it easier to read.
5. Define a function that removes the units from a united variable or scalar and use it to define the vertical markers as the unitless elements of bins1.
 NoU x
x SIUnitsOf x
m10 NoU bins1 0 m11 NoU bins1 1 m12 NoU bins1 2 m13 NoU bins1 3 m14 NoU bins1 4
6. Plot a bi-histogram of the data sets, and then add vertical markers to show the mid point of each bin.
As can be seen in the plot:
The number of bins equals one less than the length of the intervals vector.
The V1 and V2 hist output vectors are represented by the bars above and below the horizontal axis, respectively.
As the vertical markers show, each bar is centered around its corresponding bin value.
Specifying an Integer to Indicate Number of Intervals
1. Define the interval parameter that represents the number of subintervals of equal lengths.
2. Use the histogram function to create the two new histograms.
3. Use the hist function to count the number of elements of V1 and V2 contained in each bin.
 R21 hist P V1 R22 hist P V2
The hist function now returns result vectors of length P for V1 and V2.
The sum of elements in each vector adds up to the number of elements in V1 and V2.
4. Define the x-axis as column 0 of H21, normalized by 108 to make it easier to read.
5. Extract the unitless values from bins2 and use them to plot the vertical markers that mark the mid point of each bin.
 m20 NoU bins2 0 m21 NoU bins2 1 m22 NoU bins2 2 m23 NoU bins2 3 m24 NoU bins2 4 m25 NoU bins2 5
6. Plot the new bi-histogram of the data sets.
As can be seen in the plot:
The number of bins equals the interval parameter P.
The V1 and V2 hist output vector is represented by the bars above and below the horizontal axis, respectively.
As the vertical markers show, each bar is centered around its corresponding bin value.