Advanced Hexbin Plots

2D hexagonal binning with absolute values and value overlay visualization.

Best for: Detailed density visualization with actual values and custom aggregation

import matplotlib.pyplot as plt
import numpy as np
import diive as dv

Load and prepare data

df = dv.load_exampledata_parquet()

# Select growing season for cleaner visualization
df = df.loc[(df.index.month >= 5) & (df.index.month <= 9)].copy()
data = df[['Tair_f', 'VPD_f', 'NEE_CUT_REF_f']].dropna()

print(f"Loaded {len(data)} growing season records")
  > Loaded .parquet file
/home/docs/checkouts/diive/checkouts/latest/diive/configs/exampledata/exampledat
a_PARQUET_CH-DAV_FP2022.5_2013-2022_ID20230206154316_30MIN.parquet (0.052
seconds).
Loaded 73440 growing season records

Hexbin with absolute values

Use original variable values on axes with mean aggregation in each bin.

hm = dv.plotting.HexbinPlot(
    x=data['Tair_f'],
    y=data['VPD_f'],
    z=data['NEE_CUT_REF_f'],
    normalize_axes=False,              # Data: use original data values (not percentile)
    gridsize=15,                       # Data: number of hexagons per side
    reduce_C_function=np.mean          # Data: use mean aggregation (not median)
)
hm.plot(
    ax=None,                           # Create new figure
    # Chrome (title + axis labels) is described once by a shared FormatStyle.
    format_style=dv.plotting.FormatStyle(
        xlabel='Air temperature (°C)',
        ylabel='Vapor pressure deficit (hPa)',
    ),
    cmap='RdYlBu_r',                   # Colorbar: colormap name
    zlabel='Mean NEE',                 # Colorbar: label (not FormatStyle chrome)
    show_values=False,                 # Don't show values on hexagons
    cb_digits_after_comma=0            # Colorbar decimal places
)

plt.show(block=False)

print("Plotted hexbin with absolute values and mean aggregation")
plot hexbin advanced
Plotted hexbin with absolute values and mean aggregation

Hexbin with value overlay

Display aggregated values directly on hexagon centers for detailed readability.

hm = dv.plotting.HexbinPlot(
    x=data['Tair_f'],
    y=data['VPD_f'],
    z=data['NEE_CUT_REF_f'],
    normalize_axes=True,               # Data: use percentile scale (0-100)
    gridsize=20,                       # Data: number of hexagons per side
    reduce_C_function=np.mean          # Data: use mean aggregation
)
hm.plot(
    ax=None,                           # Create new figure
    # Chrome (title + axis labels) is described once by a shared FormatStyle.
    format_style=dv.plotting.FormatStyle(
        xlabel='Temperature (percentile)',
        ylabel='VPD (percentile)',
    ),
    cmap='RdYlBu_r',                   # Colorbar: colormap name
    zlabel='Mean NEE',                 # Colorbar: label (not FormatStyle chrome)
    show_values=True,                  # Display values on hexagon centers
    show_values_fontsize=8,            # Font size for overlay values
    show_values_n_dec_places=0,        # Decimal places in overlay
    cb_digits_after_comma=1            # Colorbar decimal places
)

plt.show(block=False)

print("Plotted hexbin with value overlays on hexagon centers")
plot hexbin advanced
Plotted hexbin with value overlays on hexagon centers

Total running time of the script: (0 minutes 0.624 seconds)

Gallery generated by Sphinx-Gallery