# 5 Best Ways to Display a Hexbin Plot in Python Using Seaborn Library

Rate this post

π‘ Problem Formulation: When dealing with large datasets containing bivariate data, scatter plots can become cluttered and less informative. A hexbin plot merges points into hexagonal bins, providing a clear visualization of the density distribution. This article provides five methods to use the Seaborn library for creating informative hexbin plots in Python, assuming you have a set of x and y data points for which you want to visualize the density distribution.

## Method 1: Basic Hexbin Plot

This method covers how to create a basic hexbin plot in Seaborn to visualize the density of points. The `seaborn.jointplot()` function is used with the `kind='hex'` parameter to generate the plot.

Here’s an example:

```import seaborn as sns
import matplotlib.pyplot as plt

# Sample data
x, y = sns.load_dataset('diamonds')[['carat', 'price']].T

# Create a basic hexbin plot
sns.jointplot(x=x, y=y, kind='hex')
plt.show()```

The output is a hexbin plot displaying the density of points where ‘x’ and ‘y’ values intersect on a 2D plane.

This snippet loads a sample dataset of diamonds, selects carat and price for the ‘x’ and ‘y’ data, respectively, and then uses `seaborn.jointplot()` with the `kind` parameter set to `'hex'` to create and display the hexbin plot.

## Method 2: Custom Bin Size

Adjusting the size of hexbins can provide a different perspective on data density. The `gridsize` parameter within the `seaborn.jointplot()` function allows customization of hexbin sizes.

Here’s an example:

```# Using previously loaded data from Method 1

# Create a hexbin plot with custom bin size
sns.jointplot(x=x, y=y, kind='hex', gridsize=30)
plt.show()```

The resulting plot has hexagons that are larger or smaller depending on the specified `gridsize`, affecting the granularity of the data visualization.

This code modifies the hexbin size by setting the `gridsize` parameter. A gridsize of 30 is used in this example, which increases or decreases the size of the hexagons depending on the chosen value, consequently changing how data density is visualized.

## Method 3: Adding Color Maps

Color maps can enhance the visualization of density differences in a hexbin plot. Seaborn provides a `cmap` parameter that applies a matplotlib colormap to the hexbin plot.

Here’s an example:

```# Using previously loaded data from Method 1

# Create a hexbin plot with a color map
sns.jointplot(x=x, y=y, kind='hex', cmap='viridis')
plt.show()```

The output is a hexbin plot with varying colors representing different densities of points.

By specifying a `cmap` such as ‘viridis’, the plot’s hexbins are color-coded to reflect the density of points, with darker shades typically representing higher concentrations.

## Method 4: Overlaying with a Scatter Plot

For additional context, a scatter plot can be overlaid on a hexbin plot. This is achieved by using the `plot_joint` method from the `JointGrid` object that `seaborn.jointplot()` returns.

Here’s an example:

```# Using previously loaded data from Method 1

# Create a hexbin plot with scatter plot overlay
joint_plot = sns.jointplot(x=x, y=y, kind='hex')
joint_plot.plot_joint(plt.scatter, color='white', s=5, edgecolor='blue')
plt.show()```

The output is a hexbin plot with a superimposed scatter plot, providing an additional layer of information about individual data points.

Through the `plot_joint` method, additional plotting functions such as `plt.scatter` can be called to overlay the scatter plot onto the hexbin plot, enhancing the representation of data distribution and individual data points.

## Bonus One-Liner Method 5: Hexbin Plot with DataFrames

The seaborn library can integrate seamlessly with Pandas DataFrames, enabling a hexbin plot to be created with just a single line of code using DataFrame columns.

Here’s an example:

```import pandas as pd

# Assuming 'df' is a Pandas DataFrame with 'carat' and 'price' columns

# One-liner to create hexbin plot
sns.jointplot(data=df, x='carat', y='price', kind='hex')```

The output is a hexbin plot based on the DataFrame’s ‘carat’ and ‘price’ columns.

This one-liner takes advantage of Seaborn’s integration with Pandas, plotting directly from DataFrame column names specified in the `x` and `y` parameters alongside the dataset itself as the `data` parameter.

## Summary/Discussion

• Method 1: Basic Hexbin Plot. Offers a straightforward visualization of data density. Lacks customization.
• Method 2: Custom Bin Size. Provides control over granularity. May require tweaking for optimal size.
• Method 3: Adding Color Maps. Enhances plot interpretation through visual cues. Choice of colormap is crucial.
• Method 4: Overlaying with a Scatter Plot. Adds depth to the visualization. Can become cluttered if not used judiciously.
• Method 5: Hexbin Plot with DataFrames. Quick and convenient for DataFrame users. Limited to DataFrame context.