Skip to content

MATLAB - Histograms

MATLAB - Modern Histograms for Data Analysis

Section titled “MATLAB - Modern Histograms for Data Analysis”

Histograms are essential tools in data analysis, offering a powerful graphical representation of the distribution of numerical data. They group data into ‘bins’ (intervals) and show how many data points fall into each bin. MATLAB’s modern histogram function provides a flexible and powerful way to create insightful visualizations, helping you understand data spread, central tendency, and identify anomalies.

While older versions of MATLAB used the hist function, the recommended modern approach is to use histogram. It offers more control, flexibility, and returns an object whose properties can be modified after creation.

Step-by-Step: Creating and Customizing Histograms

Section titled “Step-by-Step: Creating and Customizing Histograms”

Let’s start by visualizing the distribution of 1000 random numbers from a normal distribution. The histogram function will automatically determine an appropriate number of bins to reveal the underlying shape.

The number and width of bins can significantly change the story your histogram tells. You have several ways to control them:

  • 'NumBins': Specify the number of bins. Good for a general overview.
  • 'BinWidth': Specify the width of each bin. Useful when you have a specific interval size in mind (e.g., age groups).
  • 'BinEdges': Provide a vector that explicitly defines the edge of every bin. This gives you maximum control.

3. Normalization: Changing the Y-Axis Meaning

Section titled “3. Normalization: Changing the Y-Axis Meaning”

By default, a histogram’s bar height represents the count of data points. The 'Normalization' property allows you to represent this data differently, which is crucial for comparing datasets of different sizes.

  • 'count' (Default): The height of the bar is the raw number of observations in the bin.
  • 'probability': The height of the bar is the count divided by the total number of observations. The sum of all bar heights is 1.
  • 'pdf' (Probability Density Function): The area of each bar (height * width) is the probability of an observation falling in that bin. The total area of all bars is 1. This is best for comparing distributions with different bin widths.

Histograms are not just for numbers. They are perfect for visualizing the frequency of different categories in your data.

Assigning the output of the histogram function to a variable gives you a powerful object. You can inspect its properties (like the bin counts or edges) and modify its appearance after it has been created.

  • Misleading Bins: Too few bins can over-smooth the data, hiding important features. Too many bins can create a noisy plot that’s hard to interpret. Experiment with NumBins or BinWidth to find a good balance.
  • Misinterpreting the Y-Axis: Always be mindful of the normalization used. A ‘count’ of 50 is very different from a ‘probability’ of 0.5. Label your y-axis clearly!
  • Comparing Datasets: To compare histograms of two different datasets, it’s almost always better to use 'probability' or 'pdf' normalization, especially if the datasets have different numbers of total observations.

Once you’ve mastered histogram, explore these related functions:

  • histogram2: For visualizing the joint distribution of two variables.
  • ksdensity: To compute a smooth probability density estimate, which can be a good alternative to a PDF-normalized histogram.
  • boxplot: For a different view of a distribution, focusing on quartiles and outliers.