Matplotlib 3D Histogram

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, you will learn how to create a 3D histogram of 2D data using Python Matplotlib. A histogram is a graphical representation of data that groups a range of values into bins, and the 3D histogram extends this concept by adding a third dimension to the visualization.

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`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) 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/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") 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`") subgraph Lab Skills python/comments -.-> lab-48768{{"`Matplotlib 3D Histogram`"}} python/with_statement -.-> lab-48768{{"`Matplotlib 3D Histogram`"}} matplotlib/importing_matplotlib -.-> lab-48768{{"`Matplotlib 3D Histogram`"}} matplotlib/figures_axes -.-> lab-48768{{"`Matplotlib 3D Histogram`"}} python/for_loops -.-> lab-48768{{"`Matplotlib 3D Histogram`"}} python/lists -.-> lab-48768{{"`Matplotlib 3D Histogram`"}} python/tuples -.-> lab-48768{{"`Matplotlib 3D Histogram`"}} python/importing_modules -.-> lab-48768{{"`Matplotlib 3D Histogram`"}} python/standard_libraries -.-> lab-48768{{"`Matplotlib 3D Histogram`"}} python/math_random -.-> lab-48768{{"`Matplotlib 3D Histogram`"}} python/numerical_computing -.-> lab-48768{{"`Matplotlib 3D Histogram`"}} python/data_visualization -.-> lab-48768{{"`Matplotlib 3D Histogram`"}} end

Import Libraries

Before we can create the 3D histogram, we need to import the necessary libraries. In this case, we will be using NumPy and Matplotlib.

import matplotlib.pyplot as plt
import numpy as np

Generate Data

Next, we will generate some random 2D data to use for the histogram. We will use NumPy's random.rand() function to generate 100 random values for both the x and y variables.

## Fixing random state for reproducibility
np.random.seed(19680801)

x, y = np.random.rand(2, 100) * 4

Create the Histogram

Now that we have our data, we can create the 3D histogram. We will use NumPy's histogram2d() function to create a 2D histogram of our data, and then use Matplotlib's bar3d() function to create a 3D bar graph of the histogram.

hist, xedges, yedges = np.histogram2d(x, y, bins=4, range=[[0, 4], [0, 4]])

## Construct arrays for the anchor positions of the 16 bars.
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25, indexing="ij")
xpos = xpos.ravel()
ypos = ypos.ravel()
zpos = 0

## Construct arrays with the dimensions for the 16 bars.
dx = dy = 0.5 * np.ones_like(zpos)
dz = hist.ravel()

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, zsort='average')

Display the Histogram

Finally, we can display the histogram using Matplotlib's show() function.

plt.show()

Summary

In this lab, you learned how to create a 3D histogram of 2D data using Python Matplotlib. You also learned how to generate random data, create a 2D histogram, and create a 3D bar graph of the histogram. By following these steps, you can create your own 3D histograms to visualize your own data.

Other Python Tutorials you may like