Skip to main content

Registration is now open for this year's LibreFest! Join us virtually the week of July 13.

Register here
Engineering LibreTexts

10.8: Histograms

  • Page ID
    136717
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    A histogram is a special kind of bar chart that shows how frequently values occur within ranges called bins. Histograms are useful when you want to understand the distribution of data.

    For example, a histogram can help you see whether exam scores are clustered near the middle, whether temperature measurements are spread out, or whether most sensor readings fall within a small range.

    The basic command is:

     

    histogram(data)

    Histograms are different from bar plots. A bar plot usually compares categories or specific values. A histogram groups numerical data into bins and shows frequency.

    In the example below, MATLAB creates a vector of random integers from 1 to 5 and then counts how often each value appears.

     

    Example \(\PageIndex{1}\)

    creating a vector of 100 random integers from 1 to 5 and counting how often each value appears.

    clear; clc; clf;
    vec = randi([1 5], 1, 100);
    histogram(vec)
    title('Histogram of Random Integers')
    xlabel('Value')
    ylabel('Frequency')
    
    Solution

    clipboard_ed59153305caa11f756abef2dcdedd94d.png

    Because the data is random, you may get a different histogram each time you run the script. The height of each bar shows how many values fall into that bin.

     

    Understanding Bins

    MATLAB divides the data into intervals called bins. Each bin represents a range of values. MATLAB then counts how many data values fall into each bin. In the previous example for integer data from 1 to 5, the bins naturally correspond to the integer values. For continuous data, such as temperatures in a room or voltages in a circuit, each bin represents a range of values.

    The number and width of bins can change how a histogram looks. Too few bins may hide important details. Too many bins may make the plot look noisy. MATLAB chooses bin settings automatically, but you can control them when needed.

    Example \(\PageIndex{2}\)

    Plotting a histogram of 1000 of randomly generated numbers  between 0 and 100 using 20 bins

    data = randi([0, 100], 1, 1000);
    
    histogram(data, 20)  % use 20 bins
    
    Solution

    clipboard_e3a6fb13ccc2e51efd1ec4b6b10830560.png

     

    You can also use name-value pairs for more control. For example:

    histogram(data, 'BinWidth', 0.5) %bin width is defined instead of the bin number
    Example \(\PageIndex{3}\)

    Histogram of Continuous Temperature Data.

    A temperature sensor recorded a room’s temperature once every minute for 20 minutes. 

    clear; clc; clf;
    
    temps = [68.2 69.1 70.4 71.2 69.8 72.1 73.0 71.5 70.9 69.3 ...
    68.7 72.8 74.1 73.6 70.2 69.9 71.8 72.4 73.2 70.7];
    histogram(temps, 'BinWidth', 1)
    xlabel('Temperature (°F)')
    ylabel('Number of readings')
    title('Histogram of Room Temperature Readings')
    grid on
    

     

    Solution

    clipboard_ef08cda93974993e61c34cded2380217f.png

     

    As you see in this example, the histogram does not plot each continuous data value individually. Instead, it groups values into bins and counts how many values fall into each bin. The name-value pair'BinWidth', 1 tells MATLAB to make each bin 1 degree wide. This makes the histogram easier to interpret because each bar represents a 1-degree temperature interval.This helps us see the overall distribution of the data, such as where most values are concentrated and whether there are unusual values. 

     

    When to Use a Histogram

    Use a histogram when you want to answer questions such as: What values occur most often? Are the data values spread out or clustered? Are there unusual values far away from the rest of the data?

    Note

    Older MATLAB versions used the functionhist, but histogram is the recommended function for creating histograms in newer MATLAB code.

     


    10.8: Histograms is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?