Plotting a Histogram with Matplotlib in Python Using a List of Data

Rate this post

πŸ’‘ Problem Formulation: This article addresses how to visualize the distribution of numerical data in a list by plotting a histogram in Python using the Matplotlib library. The end goal is to turn a given list of numerical values, such as [1, 2, 2, 3, 4, 5, 5, 5, 6], into a visual histogram that showcases the frequency of each unique value.

Method 1: Basic Histogram Plotting

The most straightforward method to plot a histogram using Matplotlib involves utilizing the plt.hist() function. This function automatically bins the data and plots the histogram. You can customize the appearance of the histogram by altering the function’s parameters.

Here’s an example:

import matplotlib.pyplot as plt

data = [1, 2, 2, 3, 4, 5, 5, 5, 6]

plt.title('Frequency of Numbers')

The above code snippet will display a histogram where the x-axis represents the numbers and the y-axis their frequency within the dataset.

This method is efficient for quickly visualizing data. The function plt.hist() takes care of the binning process, and with additional optional arguments, you can control the number of bins, aesthetics, and other properties of the plot.

Method 2: Customized Bins

Matplotlib allows you to customize the histogram bins. By specifying the bins parameter, you can control how the data is grouped. This is useful when you want the bins to align with certain intervals.

Here’s an example:

import matplotlib.pyplot as plt

data = [1, 2, 2, 3, 4, 5, 5, 5, 6]

plt.hist(data, bins=[0, 2, 4, 6, 8])
plt.title('Custom Bin Edges')
plt.xlabel('Number Intervals')

This code produces a histogram with custom bin edges such that the bars represent intervals between those edges.

By defining the bins parameter as a list of edges, we can force the histogram to have bins that are not necessarily of equal width, catering to specialized analysis needs or improving the readability of the plot.

Method 3: Histogram with Normalization

To compare datasets of different sizes or to convert frequencies to probabilities, the density parameter is used for normalizing the histogram. It converts the frequency count to a probability, such that the area under the histogram sums to one.

Here’s an example:

import matplotlib.pyplot as plt

data = [1, 2, 2, 3, 4, 5, 5, 5, 6]

plt.hist(data, density=True)
plt.title('Normalized Histogram')

This code snippet will demonstrate a normalized histogram, displaying the probability density instead of the frequency count.

The normalization of a histogram can give insights into the probability distribution of the data, rather than just the raw frequencies. This makes it easier to compare datasets of differing sizes on a uniform scale.

Method 4: Overlaying Multiple Histograms

For comparative data analysis, Matplotlib allows you to overlay multiple histograms. By calling plt.hist() several times before, multiple datasets can be superimposed within the same plot, each with distinct colors and legends.

Here’s an example:

import matplotlib.pyplot as plt

data1 = [1, 2, 3, 4]
data2 = [2, 3, 4, 5]

plt.hist(data1, alpha=0.5, label='Dataset 1')
plt.hist(data2, alpha=0.5, label='Dataset 2')
plt.title('Overlaid Histograms')

The code will generate a plot with two histograms overlaying each other, enabling a visual comparison between the two datasets.

Overlaid histograms are ideal for comparing similar distributions, identifying commonalities, or highlighting differences. The alpha parameter allows setting the transparency, so both histograms can be viewed simultaneously.

Bonus One-Liner Method 5: Histogram with List Comprehension

A quick one-liner to plot a histogram using a list comprehension involves inline data generation and immediate plotting. This is particularly handy for simple exploratory data analysis.

Here’s an example:

import matplotlib.pyplot as plt

plt.hist([x for x in range(10) if x % 2 == 0])

This code will create and plot a histogram of even numbers between 0 and 9.

The conciseness of this method enables rapid prototyping and visualization with minimal code. However, it lacks the readability and customization available in more expanded forms.


  • Method 1: Basic Histogram Plotting. Simple and straightforward. Can lack finer control over the histogram’s appearance without additional parameters.
  • Method 2: Customized Bins. Offers control over the distribution of bins. Could become complex if too many custom bins are required.
  • Method 3: Histogram with Normalization. Useful for probability distributions and comparisons. Not suitable when actual frequencies are needed.
  • Method 4: Overlaying Multiple Histograms. Ideal for comparison. Can be visually cluttered with too many overlapping histograms.
  • Method 5: One-Liner. Quick and easy for basic plots. Not practical for complex visualizations with nuanced requirements.