
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - Hexagonal Bin Plot
Hexagonal bin plot, also known ashexbin plot, is an alternative to scatter plots, especially when the data is too dense to visualize individual points. Instead of plotting each data point, the hexagonal bin plot divides the plane into hexagons and aggregates data within each hexagon.
The Pandas library provides theDataFrame.plot.hexbin() method for creating the hexagonal bin plot. In this tutorial, we will learn about how to use the PandasDataFrame.plot.hexbin() method for creating and customizing hexagonal bin plot with different examples.
Hexagonal Bin Plot in Pandas
By using theDataFrame.plot.hexbin() method, you can create a hexagonal bin plot for x versus y data. Resulting a Matplotlib Axes on which the hexagonal bin plot is rendered.
Syntax
Following is the syntax of the plot.hexbin() method −
DataFrame.plot.hexbin(x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs)
Where,
x: The column label or position for x-axis values.
y: The column label or position for y-axis values.
C: An Optional parameter specifies the column label or position for the value at each (x, y) point. Defaults to None.
reduce_C_function: An aggregate function to reduce bin values into a single number (e.g., np.mean, np.max, np.sum, etc.).
gridsize: Specifies the number of hexagons in the x-direction. Alternatively, a tuple can specify the number of hexagons in both x and y directions.
**kwargs: Additional keyword arguments to customize the plot.
Example
Let's create a basic hexagonal bin plot using theDataFrame.plot.hexbin() method.
import pandas as pdimport numpy as npimport matplotlib.pyplot as plt# Generate sample datadf = pd.DataFrame(np.random.randn(500, 2), columns=["Col1", "Col2"])df["Col2"] = df["Col2"] + np.arange(500)# Create a hexagonal bin plotdf.plot.hexbin(x="Col1", y="Col2", gridsize=25)# Set title and Display the plotplt.title('Basic Hexagonal Bin Plot')plt.show()Following is the output of the above code −

Applying Aggregation to the Hexbin Plot
You can apply additional data aggregation function to reduce bin values into a single number using theC andreduce_C_function parameters. The parameterC specifies the values at each (x, y) points, and thereduce_C_function parameter takes a function to aggregate values in each bin to a single number.
Example
The following example demonstrates creating a hexagonal bin plot with an additional aggregation function using theC andreduce_C_function parameters.
import pandas as pdimport numpy as npimport matplotlib.pyplot as plt# Generate sample datadf = pd.DataFrame(np.random.randn(500, 2), columns=["Col1", "Col2"])df["Col2"] = df["Col2"] + np.arange(500)# Add a value columndf["Col3"] = np.random.uniform(0, 3, 500)# Create a hexagonal bin plot with aggregationdf.plot.hexbin(x="Col1", y="Col2", C="Col3", reduce_C_function=np.max, gridsize=25)# Set title and Display the plotplt.title('Hexagonal Bin Plot with Aggregation')plt.show()On executing the above code we will get the following output −

Customizing Hexagonal Bin Plots
You can enhance the visual appeal the hexagonal bin plots by customizing parameters like colormap, transparency, edgecolors, and more.
Example
The following example demonstrates customizing the hexagonal bin plot using the colormap, transparency, and edgecolors parameters.
import pandas as pdimport numpy as npimport matplotlib.pyplot as plt# Generate sample datadf = pd.DataFrame(np.random.randn(500, 2), columns=["Col1", "Col2"])df["Col2"] = df["Col2"] + np.arange(500)# Add a value columndf["Col3"] = np.random.uniform(0, 3, 500)# Create a hexagonal bin plot with customizationsdf.plot.hexbin(x="Col1", y="Col2", gridsize=25, cmap='magma', edgecolors="black", alpha=0.7)# Set title and Display the plotplt.title('Hexagonal Bin Plot with Customization')plt.show()After executing the above code, we get the following output −
