Skip to Main Content Skip to Search
Product Documentation

plot::Histogram2dhistogram plots of data

plot::Histogram2d(data) plots a histogram showing the distribution of the data.

→ Examples

Call:

plot::Histogram2d(data, <a = amin .. amax>, Options)

Parameters:

data

the data to plot: A list of real values or expressions in the animation parameter a
data is equivalent to the attribute Data.

Related Domains:

plot::Bars2d, plot::Boxplot, plot::Scatterplot

Related Functions:

plot, plot::copy, stats::frequency

Details:

Example 1

To plot a histogram of a normally distributed process, we first create a generator of random numbers with this distribution:

X := stats::normalRandom(0, 1)

`proc X() ... end`

Next, we create a small number of “measurements”:

data := [X() $ i = 1..20]

[-0.5297400457, -0.5694234147, -0.5161446272, -1.090814471, 1.782520584, 0.6370330472, 0.6902341601, 0.3399758858, 1.177699186, -0.5970692982, -1.386247581, -0.9783222199, -0.7891413081, 0.2090732178, 0.2186783746, -0.7392138209, 0.6496128588, 0.6258699055, 3.606896706, -0.3319378999]

This data is ready to be put into plot::Histogram2d:

plot(plot::Histogram2d(data))

MuPAD graphics

This plot, if nothing else, shows that 20 samples are very few. Let us repeat the process with more data:

data := [X() $ i = 1..300]:
plot(plot::Histogram2d(data))

MuPAD graphics

On the other hand, this amount of data certainly justifies a finer classification:

plot(plot::Histogram2d(data, Cells = [15]))

MuPAD graphics

Example 2

It is also possible to give the cells (classes) directly. To do so, you should give them as ranges or lists with two elements, as in the following example:

X := stats::normalRandom(0, 1):
data := [X() $ i = 1 .. 100]:
min(data), max(data)

-3.266420216, 2.409775834

h := plot::Histogram2d(data,
            Cells = [-2.5..-1.5, -1.5..-1, -1..0,
                     0..1, 1..1.5, 1.5..2.5])

`plot::Histogram2d(...)`

plot(h)

MuPAD graphics

It is even possible to use -infinity and infinity as border values in the cells:

h::Cells := [-infinity..-2, -2..-1, -1..0,
             0..1, 1..2, 2..infinity]:
plot(h)

MuPAD graphics

Example 3

One potential problem with classes of non-equal width is that still the height of the bars corresponds to the number of data points in a class. To see why this may be a problem, consider data perfectly uniformly distributed:

data := [i/1000 $ i = 1..1000]:

Plotting a histogram of this data, we see only very small deviations from a rectangle, caused by the fact that 1000 and 7 are coprime:

plot(plot::Histogram2d(data))

MuPAD graphics

However, plotting a histogram with uneven classes, the image looks very much different:

plot(plot::Histogram2d(data,
       Cells = [0..1/2, 1/2..2/3, 2/3..3/4, 3/4..4/5, 4/5..1]))

MuPAD graphics

To make not the height, but rather the area of a bar depend on the number of samples in a class, set Area to a positive value:

plot(plot::Histogram2d(data,
       Cells = [0..1/2, 1/2..2/3, 2/3..3/4, 3/4..4/5, 4/5..1],
       Area = 1))

MuPAD graphics

Note that with Area = 1, a histogram plot is scaled accordingly to the probability density function of the variable displayed:

X := stats::normalRandom(0, 1):
data := [X() $ i = 1..1000]:
h := plot::Histogram2d(data, Cells = [15],
                       Area = 1, Color = RGB::Blue):
f := plot::Function2d(stats::normalPDF(0, 1),
                      x = -3..3, LineWidth = 1*unit::mm,
                      Color = RGB::Black):
plot(h, f)

MuPAD graphics

delete X, data, h, f:

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS