3D Scatter
Customer Segmentation (RFM Analysis)
Customer segmentation using RFM (Recency, Frequency, Monetary) analysis in 3D space.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(555)
# Customer data: RFM analysis
n_customers = 200
# 4 segments
segments = ['Champions', 'Loyal', 'At Risk', 'Lost']
colors = ['#22c55e', '#3b82f6', '#f59e0b', '#ef4444']
n_per_seg = n_customers // 4
all_r, all_f, all_m, all_c = [], [], [], []
# Champions: high R, F, M
r = np.random.uniform(80, 100, n_per_seg)
f = np.random.uniform(70, 100, n_per_seg)
m = np.random.uniform(60, 100, n_per_seg)
all_r.extend(r); all_f.extend(f); all_m.extend(m)
all_c.extend([colors[0]] * n_per_seg)
# Loyal: medium-high
r = np.random.uniform(50, 80, n_per_seg)
f = np.random.uniform(50, 80, n_per_seg)
m = np.random.uniform(40, 70, n_per_seg)
all_r.extend(r); all_f.extend(f); all_m.extend(m)
all_c.extend([colors[1]] * n_per_seg)
# At Risk: low R, medium F/M
r = np.random.uniform(20, 50, n_per_seg)
f = np.random.uniform(40, 70, n_per_seg)
m = np.random.uniform(30, 60, n_per_seg)
all_r.extend(r); all_f.extend(f); all_m.extend(m)
all_c.extend([colors[2]] * n_per_seg)
# Lost: low all
r = np.random.uniform(0, 30, n_per_seg)
f = np.random.uniform(0, 40, n_per_seg)
m = np.random.uniform(0, 40, n_per_seg)
all_r.extend(r); all_f.extend(f); all_m.extend(m)
all_c.extend([colors[3]] * n_per_seg)
fig = plt.figure(figsize=(10, 8), facecolor='#ffffff')
ax = fig.add_subplot(111, projection='3d', facecolor='#ffffff')
ax.scatter(all_r, all_f, all_m, c=all_c, s=50, alpha=0.7, edgecolors='white', linewidths=0.5)
ax.set_xlabel('Recency Score', color='#1f2937', fontsize=10)
ax.set_ylabel('Frequency Score', color='#1f2937', fontsize=10)
ax.set_zlabel('Monetary Score', color='#1f2937', fontsize=10)
ax.set_title('Customer Segmentation (RFM Analysis)', color='#1f2937', fontsize=14, fontweight='bold', pad=20)
ax.tick_params(colors='#6b7280', labelsize=8)
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False
ax.xaxis.pane.set_edgecolor('#e5e7eb')
ax.yaxis.pane.set_edgecolor('#e5e7eb')
ax.zaxis.pane.set_edgecolor('#e5e7eb')
ax.view_init(elev=20, azim=45)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
3D Charts
☕