Hexbin Plot
E-commerce Conversion Funnel
Session duration vs cart value analysis for checkout optimization.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
np.random.seed(42)
n_sessions = 10000
session_duration = np.random.lognormal(2.5, 0.9, n_sessions)
session_duration = np.clip(session_duration, 0.5, 60)
base_cart = 20 + 2 * session_duration
cart_value = base_cart * np.random.lognormal(0, 0.5, n_sessions)
cart_value = np.clip(cart_value, 5, 500)
fig, ax = plt.subplots(figsize=(10, 8), facecolor='#ffffff')
ax.set_facecolor('#fefce8')
colors = ['#fefce8', '#fef9c3', '#fef08a', '#fde047', '#facc15',
'#eab308', '#ca8a04', '#a16207', '#854d0e', '#713f12']
cmap = LinearSegmentedColormap.from_list('amber', colors, N=256)
hb = ax.hexbin(session_duration, cart_value, gridsize=35, cmap=cmap, mincnt=1,
edgecolors='white', linewidths=0.3)
ax.axvline(x=10, color='#ca8a04', linestyle='--', alpha=0.7, linewidth=2, label='Engaged (>10min)')
ax.axhline(y=100, color='#16a34a', linestyle='--', alpha=0.7, linewidth=2, label='High Value (>$100)')
from matplotlib.patches import Rectangle
target = Rectangle((10, 100), 50, 400, fill=False, edgecolor='#16a34a',
linewidth=2.5, linestyle='-', alpha=0.8)
ax.add_patch(target)
ax.text(35, 400, 'Target Segment', fontsize=10, color='#16a34a', ha='center', fontweight='600')
cbar = plt.colorbar(hb, ax=ax, pad=0.02, shrink=0.85)
cbar.set_label('Session Count', fontsize=11, color='#713f12', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#854d0e')
cbar.outline.set_edgecolor('#fef08a')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#854d0e', fontsize=9)
ax.set_xlabel('Session Duration (min)', fontsize=12, color='#713f12', fontweight='600', labelpad=12)
ax.set_ylabel('Cart Value ($)', fontsize=12, color='#713f12', fontweight='600', labelpad=12)
ax.set_title('E-commerce Session Analysis', fontsize=16, color='#451a03', fontweight='700', pad=20)
ax.tick_params(colors='#854d0e', labelsize=10, length=0)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#fef08a')
ax.spines['bottom'].set_color('#fef08a')
ax.legend(loc='upper right', fontsize=9, frameon=True, facecolor='white',
edgecolor='#fef08a', labelcolor='#713f12')
ax.grid(True, alpha=0.3, color='#fef08a', linestyle='-', linewidth=0.5)
ax.set_axisbelow(True)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Hexbin Plot examples
☕