Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

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 −

Basic Hexagonal Bin Plot

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 −

Hexbin Plot with Aggregation

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 −

Hexbin Plot with Customization
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp