Histogram
Net Promoter Score
NPS distribution with zones.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
COLORS = {'promoter': '#10B981', 'passive': '#F59E0B', 'detractor': '#EF4444', 'background': '#FFFFFF', 'text': '#1E293B', 'text_muted': '#64748B', 'grid': '#F1F5F9'}
np.random.seed(42)
# Simulate NPS: detractors (0-6), passives (7-8), promoters (9-10)
detractors = np.random.randint(0, 7, 150)
passives = np.random.randint(7, 9, 100)
promoters = np.random.randint(9, 11, 250)
scores = np.concatenate([detractors, passives, promoters])
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])
bins = np.arange(-0.5, 11.5, 1)
counts, bins_out, patches = ax.hist(scores, bins=bins, edgecolor='white', linewidth=0.5, rwidth=0.85)
for i, patch in enumerate(patches):
if i <= 6:
patch.set_facecolor(COLORS['detractor'])
elif i <= 8:
patch.set_facecolor(COLORS['passive'])
else:
patch.set_facecolor(COLORS['promoter'])
patch.set_alpha(0.85)
# NPS calculation
nps = (len(promoters) - len(detractors)) / len(scores) * 100
ax.text(0.97, 0.95, f'NPS: {nps:.0f}', transform=ax.transAxes, fontsize=12,
fontweight='bold', va='top', ha='right', color=COLORS['text'])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color(COLORS['grid'])
ax.spines['bottom'].set_color(COLORS['grid'])
ax.yaxis.grid(True, color=COLORS['grid'], linewidth=1, zorder=0)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax.set_xticks(range(11))
ax.set_xlabel('Score (0-10)', fontsize=10, color=COLORS['text'], labelpad=10)
ax.set_ylabel('Responses', fontsize=10, color=COLORS['text'], labelpad=10)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Basic Charts
More Histogram examples
☕