Hexbin Plot
API Latency Distribution
Request payload size vs response latency for performance optimization.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
np.random.seed(42)
n_requests = 15000
payload_size = np.random.lognormal(6, 1.2, n_requests)
payload_size = np.clip(payload_size, 10, 10000)
base_latency = 20 + 0.01 * payload_size
latency = base_latency + np.random.exponential(15, n_requests)
latency = np.clip(latency, 5, 500)
fig, ax = plt.subplots(figsize=(10, 8), facecolor='#ffffff')
ax.set_facecolor('#f8fafc')
colors = ['#f8fafc', '#f1f5f9', '#e2e8f0', '#cbd5e1', '#94a3b8',
'#64748b', '#475569', '#334155', '#1e293b', '#0f172a']
cmap = LinearSegmentedColormap.from_list('slate', colors, N=256)
hb = ax.hexbin(payload_size, latency, gridsize=40, cmap=cmap, mincnt=1,
edgecolors='white', linewidths=0.3)
ax.axhline(y=100, color='#22c55e', linestyle='--', alpha=0.8, linewidth=2, label='SLA Target (100ms)')
ax.axhline(y=200, color='#f59e0b', linestyle=':', alpha=0.8, linewidth=2, label='Warning (200ms)')
ax.axhline(y=300, color='#ef4444', linestyle='--', alpha=0.7, linewidth=2, label='Critical (300ms)')
cbar = plt.colorbar(hb, ax=ax, pad=0.02, shrink=0.85)
cbar.set_label('Request Count', fontsize=11, color='#1e293b', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#334155')
cbar.outline.set_edgecolor('#e2e8f0')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#334155', fontsize=9)
ax.set_xlabel('Payload Size (bytes)', fontsize=12, color='#1e293b', fontweight='600', labelpad=12)
ax.set_ylabel('Latency (ms)', fontsize=12, color='#1e293b', fontweight='600', labelpad=12)
ax.set_title('API Performance Analysis', fontsize=16, color='#0f172a', fontweight='700', pad=20)
ax.tick_params(colors='#334155', labelsize=10, length=0)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#e2e8f0')
ax.spines['bottom'].set_color('#e2e8f0')
ax.legend(loc='upper left', fontsize=9, frameon=True, facecolor='white',
edgecolor='#e2e8f0', labelcolor='#1e293b')
ax.grid(True, alpha=0.3, color='#e2e8f0', linestyle='-', linewidth=0.5)
ax.set_axisbelow(True)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Hexbin Plot examples
☕