Building Histograms With Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will learn how to use Matplotlib to build histograms using rectangles and PolyCollections. We will use numpy to generate random data, and then use Matplotlib to visualize the data as a histogram. This lab assumes that you have a basic understanding of Python and Matplotlib.

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`"]) 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`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") 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 matplotlib/importing_matplotlib -.-> lab-48773{{"`Building Histograms With Matplotlib`"}} matplotlib/figures_axes -.-> lab-48773{{"`Building Histograms With Matplotlib`"}} python/variables_data_types -.-> lab-48773{{"`Building Histograms With Matplotlib`"}} python/numeric_types -.-> lab-48773{{"`Building Histograms With Matplotlib`"}} python/lists -.-> lab-48773{{"`Building Histograms With Matplotlib`"}} python/tuples -.-> lab-48773{{"`Building Histograms With Matplotlib`"}} python/importing_modules -.-> lab-48773{{"`Building Histograms With Matplotlib`"}} python/standard_libraries -.-> lab-48773{{"`Building Histograms With Matplotlib`"}} python/math_random -.-> lab-48773{{"`Building Histograms With Matplotlib`"}} python/numerical_computing -.-> lab-48773{{"`Building Histograms With Matplotlib`"}} python/data_visualization -.-> lab-48773{{"`Building Histograms With Matplotlib`"}} python/build_in_functions -.-> lab-48773{{"`Building Histograms With Matplotlib`"}} end

Import the necessary libraries

Before we start, we need to import the necessary libraries. We will be using Matplotlib and numpy in this lab. Open a new Python file and add the following code:

import matplotlib.pyplot as plt
import numpy as np

Set the random seed and generate data

We will use numpy to generate random data. In order to make our results reproducible, we will set a random seed. Add the following code to your file:

np.random.seed(19680801)
data = np.random.randn(1000)

Generate the histogram data

Now that we have our random data, we can generate a histogram using numpy. We will use 50 bins to create our histogram. Add the following code:

n, bins = np.histogram(data, 50)

Generate the corners of the rectangles

In order to draw our histogram using rectangles, we need to calculate the corners of each rectangle. Add the following code:

left = bins[:-1]
right = bins[1:]
bottom = np.zeros(len(left))
top = bottom + n

Generate the Path object and make a patch out of it

Next, we will generate a Path object and make a patch out of it. We will use the Path object to draw our histogram using rectangles. Add the following code:

XY = np.array([[left, left, right, right], [bottom, top, top, bottom]]).T
barpath = path.Path.make_compound_path_from_polys(XY)
patch = patches.PathPatch(barpath)
patch.sticky_edges.y[:] = [0]
axs[0].add_patch(patch)
axs[0].autoscale_view()

Draw the histogram using a PathCollection

Instead of using lots of Rectangle instances, we can use a faster method of drawing our histogram using a PathCollection. We will create a compound path directly using vertices and codes. Add the following code:

nrects = len(left)
nverts = nrects*(1+3+1)
verts = np.zeros((nverts, 2))
codes = np.ones(nverts, int) * path.Path.LINETO
codes[0::5] = path.Path.MOVETO
codes[4::5] = path.Path.CLOSEPOLY
verts[0::5, 0] = left
verts[0::5, 1] = bottom
verts[1::5, 0] = left
verts[1::5, 1] = top
verts[2::5, 0] = right
verts[2::5, 1] = top
verts[3::5, 0] = right
verts[3::5, 1] = bottom

barpath = path.Path(verts, codes)
patch = patches.PathPatch(barpath)
patch.sticky_edges.y[:] = [0]
axs[1].add_patch(patch)
axs[1].autoscale_view()

Display the histogram

Finally, we can display our histogram using Matplotlib. Add the following code to your file:

plt.show()

Summary

In this lab, we learned how to use Matplotlib to build histograms using rectangles and PolyCollections. We used numpy to generate random data, and then used Matplotlib to visualize the data as a histogram. We also learned how to draw histograms using a PathCollection, which is a faster method than using lots of Rectangle instances.

Other Python Tutorials you may like