Create Histograms with Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

In this tutorial, we will learn how to create a histogram using Matplotlib library. A histogram is a graphical representation of the distribution of a dataset. It is an estimate of the probability distribution of a continuous variable. To create a histogram, we need to split the entire range of values into a series of intervals or bins, and then count how many values fall into each interval.

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 matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) 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`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/histograms("`Histograms`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") 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`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48771{{"`Create Histograms with Matplotlib`"}} matplotlib/figures_axes -.-> lab-48771{{"`Create Histograms with Matplotlib`"}} matplotlib/histograms -.-> lab-48771{{"`Create Histograms with Matplotlib`"}} python/booleans -.-> lab-48771{{"`Create Histograms with Matplotlib`"}} python/lists -.-> lab-48771{{"`Create Histograms with Matplotlib`"}} python/tuples -.-> lab-48771{{"`Create Histograms with Matplotlib`"}} python/importing_modules -.-> lab-48771{{"`Create Histograms with Matplotlib`"}} python/standard_libraries -.-> lab-48771{{"`Create Histograms with Matplotlib`"}} python/math_random -.-> lab-48771{{"`Create Histograms with Matplotlib`"}} python/numerical_computing -.-> lab-48771{{"`Create Histograms with Matplotlib`"}} python/data_visualization -.-> lab-48771{{"`Create Histograms with Matplotlib`"}} end

Import the necessary libraries

First, we need to import the necessary libraries, which are NumPy and Matplotlib.

import matplotlib.pyplot as plt
import numpy as np

Generate random data

We will generate two sets of random data using NumPy's random.normal function. These sets will be used to create histograms with different styles.

np.random.seed(19680801)

mu_x = 200
sigma_x = 25
x = np.random.normal(mu_x, sigma_x, size=100)

mu_w = 200
sigma_w = 10
w = np.random.normal(mu_w, sigma_w, size=100)

Create a basic histogram

We will create a basic histogram using the hist function from Matplotlib. This histogram will have 10 equally sized bins.

plt.hist(x, bins=10)
plt.show()

Change the number of bins

We can change the number of bins by specifying the bins parameter in the hist function. In this example, we will create a histogram with 20 bins.

plt.hist(x, bins=20)
plt.show()

Change the histogram style

We can change the style of the histogram by specifying the histtype parameter in the hist function. In this example, we will create a histogram with a step curve that has a color fill.

plt.hist(x, bins=20, density=True, histtype='stepfilled', facecolor='g', alpha=0.75)
plt.show()

Create a histogram with custom bin widths

We can create a histogram with custom and unequal bin widths by providing a list of bin edges. In this example, we will create a histogram with unevenly spaced bins.

bins = [100, 150, 180, 195, 205, 220, 250, 300]
plt.hist(x, bins=bins, density=True, histtype='bar', rwidth=0.8)
plt.show()

Create two histograms with stacked bars

We can create two histograms with stacked bars by calling the hist function twice and setting the histtype parameter to 'barstacked'. In this example, we will create two histograms with stacked bars.

plt.hist(x, density=True, histtype='barstacked', rwidth=0.8)
plt.hist(w, density=True, histtype='barstacked', rwidth=0.8)
plt.show()

Summary

In this tutorial, we learned how to create a histogram using Matplotlib library. We also learned how to change the number of bins, the style of the histogram, and how to create a histogram with custom bin widths and stacked bars.

Other Python Tutorials you may like