Hexbin Plot

Customer Segmentation Analysis

RFM analysis showing customer purchase frequency vs monetary value with modern light styling.

Output
Customer Segmentation Analysis
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

# Customer RFM data
np.random.seed(42)
n_customers = 8000

# Purchase frequency and monetary value
frequency = np.random.lognormal(1.5, 0.8, n_customers)
frequency = np.clip(frequency, 1, 100)

monetary = frequency * np.random.lognormal(3, 0.6, n_customers)
monetary = np.clip(monetary, 10, 10000)

# Modern light theme
fig, ax = plt.subplots(figsize=(10, 8), facecolor='#ffffff')
ax.set_facecolor('#fafbfc')

# Soft pastel colormap
colors = ['#fafbfc', '#e8f4f8', '#d0e8f0', '#a8d8ea', '#72c3dc', 
          '#4aa8c7', '#3d8eb3', '#2d7399', '#1d5980', '#0d3f66']
cmap = LinearSegmentedColormap.from_list('soft_blue', colors, N=256)

# Hexbin plot
hb = ax.hexbin(frequency, monetary, gridsize=35, cmap=cmap, mincnt=1,
               edgecolors='white', linewidths=0.3)

# Styled colorbar
cbar = plt.colorbar(hb, ax=ax, pad=0.02, shrink=0.85)
cbar.set_label('Customer Count', fontsize=11, color='#374151', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#6b7280')
cbar.outline.set_edgecolor('#e5e7eb')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#6b7280', fontsize=9)

# High-value segment
from matplotlib.patches import Rectangle
vip = Rectangle((30, 3000), 70, 7000, fill=False, edgecolor='#ef4444', 
                linewidth=2, linestyle='--', alpha=0.8)
ax.add_patch(vip)
ax.text(65, 8500, 'VIP Segment', fontsize=10, color='#ef4444', ha='center', fontweight='600')

# Labels with modern typography
ax.set_xlabel('Purchase Frequency', fontsize=12, color='#374151', fontweight='600', labelpad=12)
ax.set_ylabel('Monetary Value ($)', fontsize=12, color='#374151', fontweight='600', labelpad=12)
ax.set_title('Customer Segmentation Analysis', fontsize=16, color='#111827', 
             fontweight='700', pad=20)

# Clean minimal axes
ax.tick_params(colors='#6b7280', labelsize=10, length=0)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#e5e7eb')
ax.spines['bottom'].set_color('#e5e7eb')

# Subtle grid
ax.grid(True, alpha=0.3, color='#e5e7eb', linestyle='-', linewidth=0.5)
ax.set_axisbelow(True)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Pairwise Data

Did this help you?

Support PyLucid to keep it free & growing

Support