2D Histogram
Manufacturing Defect Analysis
Quality control visualization showing defect rate vs production speed in industrial manufacturing.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
# Manufacturing data
np.random.seed(42)
n_batches = 10000
speed = np.random.uniform(50, 200, n_batches)
base_defect = 0.02 + 0.0004 * (speed - 50) ** 1.2
defect_rate = base_defect + np.random.exponential(0.005, n_batches)
defect_rate = np.clip(defect_rate, 0, 0.25) * 100
# Industrial dark theme
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(10, 8))
fig.patch.set_facecolor('#0a0c10')
ax.set_facecolor('#0a0c10')
# Custom colormap - industrial orange
colors = ['#0a0c10', '#101820', '#182830', '#204040', '#285850',
'#307060', '#408870', '#50a080', '#70c890', '#90f0a0', '#b0ffb0']
cmap = LinearSegmentedColormap.from_list('industrial', colors, N=256)
# 2D histogram
h = ax.hist2d(speed, defect_rate, bins=50, cmap=cmap, cmin=1)
# Quality thresholds
ax.axhline(y=2, color='#50a080', linestyle='--', alpha=0.7, linewidth=2, label='Six Sigma (<2%)')
ax.axhline(y=5, color='#ffa502', linestyle=':', alpha=0.7, linewidth=1.5, label='Acceptable (<5%)')
# Optimal zone
ax.axvspan(80, 120, alpha=0.15, color='#50a080', label='Optimal Speed')
# Trend curve
z = np.polyfit(speed, defect_rate, 2)
p = np.poly1d(z)
x_curve = np.linspace(50, 200, 100)
ax.plot(x_curve, p(x_curve), '--', color='#ff6b6b', linewidth=2, alpha=0.8)
# Styled colorbar
cbar = plt.colorbar(h[3], ax=ax, pad=0.02, shrink=0.85)
cbar.ax.set_facecolor('#0a0c10')
cbar.set_label('Batch Count', fontsize=11, color='#a0e0b0', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#a0e0b0')
cbar.outline.set_edgecolor('#2a4030')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#a0e0b0', fontsize=9)
ax.set_xlabel('Production Speed (units/hr)', fontsize=13, color='#a0e0b0', fontweight='600', labelpad=10)
ax.set_ylabel('Defect Rate (%)', fontsize=13, color='#a0e0b0', fontweight='600', labelpad=10)
ax.set_title('Production Speed vs Defect Rate', fontsize=16, color='white', fontweight='bold', pad=20)
ax.tick_params(colors='#80c090', labelsize=10, length=0)
for spine in ax.spines.values():
spine.set_visible(False)
ax.legend(loc='upper left', fontsize=9, facecolor='#1a2820', edgecolor='#2a4030', labelcolor='#a0e0b0')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More 2D Histogram examples
☕