Hexagonal Binned Plot

PythonPythonBeginner
Practice Now

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

Introduction

This tutorial will guide you through creating a hexagonal binned plot using Matplotlib in Python. Hexagonal binned plots are 2D histogram plots in which the bins are hexagons and the color represents the number of data points within each bin. They are useful for visualizing the distribution of large 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 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/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/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") 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-48765{{"`Hexagonal Binned Plot`"}} matplotlib/figures_axes -.-> lab-48765{{"`Hexagonal Binned Plot`"}} python/variables_data_types -.-> lab-48765{{"`Hexagonal Binned Plot`"}} python/tuples -.-> lab-48765{{"`Hexagonal Binned Plot`"}} python/importing_modules -.-> lab-48765{{"`Hexagonal Binned Plot`"}} python/standard_libraries -.-> lab-48765{{"`Hexagonal Binned Plot`"}} python/math_random -.-> lab-48765{{"`Hexagonal Binned Plot`"}} python/data_collections -.-> lab-48765{{"`Hexagonal Binned Plot`"}} python/numerical_computing -.-> lab-48765{{"`Hexagonal Binned Plot`"}} python/data_visualization -.-> lab-48765{{"`Hexagonal Binned Plot`"}} python/build_in_functions -.-> lab-48765{{"`Hexagonal Binned Plot`"}} end

Import Libraries

To create a hexagonal binned plot, we need to import the following libraries:

  • matplotlib.pyplot for creating the plot
  • numpy for generating random data
import matplotlib.pyplot as plt
import numpy as np

Generate Data

We will generate 100,000 data points using numpy.random.standard_normal() and numpy.random.standard_normal(). standard_normal() generates random numbers from a standard normal distribution with a mean of 0 and a standard deviation of 1.

np.random.seed(19680801)

n = 100_000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
xlim = x.min(), x.max()
ylim = y.min(), y.max()

Create the Hexagonal Binned Plot

We will create the hexagonal binned plot using matplotlib.pyplot.hexbin().

fig, ax = plt.subplots(figsize=(9, 4))

hb = ax.hexbin(x, y, gridsize=50, cmap='inferno')
ax.set(xlim=xlim, ylim=ylim)
ax.set_title("Hexagon binning")

cb = fig.colorbar(hb, ax=ax, label='counts')

Here, we set the grid size to 50 and the color map to 'inferno'. We also add a color bar to show the count of data points within each hexagon.

Add a Logarithmic Color Scale

We can add a logarithmic color scale to the hexagonal binned plot by setting bins='log' in hexbin().

fig, ax = plt.subplots(figsize=(9, 4))

hb = ax.hexbin(x, y, gridsize=50, bins='log', cmap='inferno')
ax.set(xlim=xlim, ylim=ylim)
ax.set_title("With a log color scale")

cb = fig.colorbar(hb, ax=ax, label='log10(N)')

Display the Plot

Finally, we display the plot using plt.show().

plt.show()

Summary

In this tutorial, we learned how to create a hexagonal binned plot using matplotlib.pyplot.hexbin() in Python. We generated random data using numpy.random.standard_normal() and numpy.random.standard_normal(), created the hexagonal binned plot, added a logarithmic color scale, and displayed the plot using plt.show().

Other Python Tutorials you may like