Producing Multiple Histograms with Matplotlib

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Histograms are a great way to visualize the distribution of a dataset, and Matplotlib is one of the most popular Python libraries for creating data visualizations. In this lab, we will use Matplotlib to create side-by-side histograms for multiple datasets.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48842{{"`Producing Multiple Histograms with Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48842{{"`Producing Multiple Histograms with Matplotlib`"}} matplotlib/figures_axes -.-> lab-48842{{"`Producing Multiple Histograms with Matplotlib`"}} python/for_loops -.-> lab-48842{{"`Producing Multiple Histograms with Matplotlib`"}} python/lists -.-> lab-48842{{"`Producing Multiple Histograms with Matplotlib`"}} python/tuples -.-> lab-48842{{"`Producing Multiple Histograms with Matplotlib`"}} python/importing_modules -.-> lab-48842{{"`Producing Multiple Histograms with Matplotlib`"}} python/standard_libraries -.-> lab-48842{{"`Producing Multiple Histograms with Matplotlib`"}} python/math_random -.-> lab-48842{{"`Producing Multiple Histograms with Matplotlib`"}} python/numerical_computing -.-> lab-48842{{"`Producing Multiple Histograms with Matplotlib`"}} python/data_visualization -.-> lab-48842{{"`Producing Multiple Histograms with Matplotlib`"}} python/build_in_functions -.-> lab-48842{{"`Producing Multiple Histograms with Matplotlib`"}} end

Import necessary libraries

First, we need to import the necessary libraries for our code. We will use Matplotlib and NumPy for our histograms:

import matplotlib.pyplot as plt
import numpy as np

Create example datasets

Next, we will create example datasets to use for our histograms. We will create three datasets with 387 data points each:

np.random.seed(19680801)
number_of_data_points = 387
labels = ["A", "B", "C"]
data_sets = [np.random.normal(0, 1, number_of_data_points),
             np.random.normal(6, 1, number_of_data_points),
             np.random.normal(-3, 1, number_of_data_points)]

Compute quantities for plotting

Before we can create our histograms, we need to compute some quantities for plotting. We will compute the range of our datasets, the binned data sets, the maximum bin values, and the x-locations for each histogram:

hist_range = (np.min(data_sets), np.max(data_sets))
number_of_bins = 20
binned_data_sets = [
    np.histogram(d, range=hist_range, bins=number_of_bins)[0]
    for d in data_sets
]
binned_maximums = np.max(binned_data_sets, axis=1)
x_locations = np.arange(0, sum(binned_maximums), np.max(binned_maximums))

Plot the histograms

Now that we have computed the necessary quantities for plotting, we can create our histograms. We will use the barh method to plot horizontal bars for each histogram:

## The bin_edges are the same for all of the histograms
bin_edges = np.linspace(hist_range[0], hist_range[1], number_of_bins + 1)
heights = np.diff(bin_edges)
centers = bin_edges[:-1] + heights / 2

## Cycle through and plot each histogram
fig, ax = plt.subplots()
for x_loc, binned_data in zip(x_locations, binned_data_sets):
    lefts = x_loc - 0.5 * binned_data
    ax.barh(centers, binned_data, height=heights, left=lefts)

ax.set_xticks(x_locations, labels)
ax.set_ylabel("Data values")
ax.set_xlabel("Data sets")

Display the histograms

Finally, we can display our histograms using the show method:

plt.show()

Summary

In this lab, we learned how to create side-by-side histograms for multiple datasets using Matplotlib. We computed the necessary quantities for plotting, and used the barh method to create horizontal bars for each histogram. With these skills, we can create informative visualizations of our data to gain insights and communicate our findings to others.

Other Python Tutorials you may like