2D Histogram
Customer Age vs Spending
2D histogram showing spending patterns across different age groups.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
np.random.seed(42)
# Customer spending data
ages = np.concatenate([
np.random.normal(25, 5, 2000),
np.random.normal(35, 8, 3000),
np.random.normal(55, 10, 1500)
])
spending = 100 + ages * 15 + np.random.exponential(200, len(ages))
fig, ax = plt.subplots(figsize=(10, 8), facecolor='#020B14')
ax.set_facecolor('#020B14')
# Custom colormap: mint to cyan
colors = ['#020B14', '#0d2d2d', '#27F5B0', '#27D3F5']
cmap = LinearSegmentedColormap.from_list('mint_cyan', colors, N=256)
h = ax.hist2d(ages, spending, bins=40, cmap=cmap, cmin=1)
cbar = plt.colorbar(h[3], ax=ax, pad=0.02)
cbar.set_label('Customers', color='white', fontsize=11)
cbar.ax.yaxis.set_tick_params(color='white')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='white')
ax.set_xlabel('Age', fontsize=11, color='white', fontweight='500')
ax.set_ylabel('Monthly Spending ($)', fontsize=11, color='white', fontweight='500')
ax.set_title('Customer Age vs Spending', fontsize=14, color='white', fontweight='bold', pad=15)
ax.tick_params(colors='white', labelsize=9)
for spine in ax.spines.values():
spine.set_color('#333333')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More 2D Histogram examples
☕