Plot a histogram
histo(x) histo(x) histo(x,n) histo(...,Name,Value) h=histo(...) [h,edges]=histo(...)
a 1-by-p or p-by-1 matrix of doubles, the data
a 1-by-1 matrix of doubles, the number of bins (default is Sturges rule : n = ceil(log2(p)+1), where p is the size of x. This is Sturges' rule.). If n is not a scalar, it contains the edges of the classes, in increasing order.
a string, the option to configure. Can be equal to "Normalization", "BinMethod" or "FaceColor".
a string or a matrix of doubles, the value corresponding to the option Name
.
a 1-by-(n+1) matrix of doubles, the edges of the classes of the histogram
a 1-by-n matrix of doubles, h(i) is the number of values in x that belong to [edges(i), edges(i+1)[
Compute and plots a histogram of the data in x.
Any input argument equal to the empty matrix is replaced by its default value.
[h,edges]=histo(...) computes the histogram without plotting.
histo(Name,Value) allows to configure the option Name
with the value Value
.
Available options
"Normalization"
: the kind of histogram we compute.
"count"
(default) : each height is equal to the number of
observations in the corresponding bin.
"pdf"
: normalize the histogram so that the
integral of the data is equal to 1.
"cdf"
: plot the empirical cumulated distribution function
of the data.
"BinMethod"
: the method to compute the edges.
"scott"
(default) : the number of bins is computed
with Scott's rule. The bin width is h=3.5*sigma/(p**(1/3)).
Scott's rule is good for normally distributed data.
Indeed, it minimizes the integrated mean squared error
of the density estimate.
"sturges"
: the number of bins is computed
with Sturges rule : n=ceil(log2(p)+1).
"integers"
: consider the data as integers.
The number of bins is the number of unique values in the
data.
Consider the unique values as the centers of the bins.
If the "BinMethod"
option is used, then
the number of bins n
cannot be passed to
the histo function, because using both options at the same time
is not consistent.
In other words, the calling sequence
histo(x,n,"BinMethod",value)
"FaceColor"
: the color of the bars, as a string or a RGB row
vector with 3 entries.
Either the long (e.g. "red") or the short (e.g. "r") color can be specified. The following colors are available :
"yellow" "y" "magenta" "m" "cyan" "c" "red" "r" "green" "g" "blue" "b" "white" "w" "black" "k"
If the RGB array is provided as a [R,G,B] row vector, that color is used.
Compatibility notes
The advantage of the stixbox/histo function over the Scilab/histplot function is that the number of classes n can be automatically computed in histo, while it is the first argument of histplot.
x=distfun_chi2rnd(3,1000,1); scf(); histo(x); xtitle("Chi-Square random numbers","X","Frequency") // Set the number of classes scf(); histo(x,10); // Set the edges scf(); X=distfun_unifrnd(0,1,1000,1); edges = 0:0.2:1.; histo(X,edges); // See with "count" Normalization scf(); subplot(1,2,1) histo(x,"Normalization","count"); // See with "pdf" Normalization subplot(1,2,2) histo(x,"Normalization","pdf"); x=linspace(0,15); y=distfun_chi2pdf(x,3); plot(x,y); // See with "cdf" Normalization scf(); histo(x,"Normalization","cdf"); // See various colors and styles scf(); subplot(1,3,1) histo(x,"FaceColor","b"); subplot(1,3,2) histo(x,"FaceColor","blue"); subplot(1,3,3) histo(x,"FaceColor",[255,128,128]); // Compute only histogram data // (do not produce the graphics) x=[0.4112 -0.2789 -0.23 0.0549 -1.0266]; [h,edges]=histo(x,4) // // Test BinMethod="integers" scf(); pr=0.7; N=10000; R=distfun_geornd(pr,1,N); histo(R,"BinMethod","integers","Normalization","pdf"); x=0:6; y = distfun_geopdf(x,pr); plot(x,y,"ro-") legend(["N=1000","PDF"]); xlabel("X") ylabel("P") title("Geometric Distribution pr=0.7") // // Compare Sturges vs Scott data=distfun_normrnd(0,1,1000,1); scf(); subplot(1,2,1) histo(data,"BinMethod","scott","Normalization","pdf"); title("Scott''s rule") subplot(1,2,2) histo(data,"BinMethod","sturges","Normalization","pdf"); title("Sturges''s rule") | ![]() | ![]() |