2D Histogram
Blood Pressure Distribution
2D histogram showing systolic vs diastolic blood pressure distribution in a clinical population study.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
# Clinical blood pressure data
np.random.seed(42)
n_patients = 8000
systolic = np.random.normal(125, 18, n_patients)
diastolic = 0.55 * systolic + np.random.normal(10, 8, n_patients)
systolic = np.clip(systolic, 90, 180)
diastolic = np.clip(diastolic, 55, 115)
# Medical dark theme
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(10, 8))
fig.patch.set_facecolor('#080810')
ax.set_facecolor('#080810')
# Custom colormap - medical teal-pink
colors = ['#080810', '#0a1a28', '#1a3a48', '#2a5a68', '#3a7a88',
'#5a9aa8', '#7abac8', '#9adae8', '#baf0f8', '#daffff', '#ffffff']
cmap = LinearSegmentedColormap.from_list('medical', colors, N=256)
# 2D histogram
h = ax.hist2d(systolic, diastolic, bins=50, cmap=cmap, cmin=1)
# Hypertension zones
ax.axvline(x=140, color='#ff6b6b', linestyle='--', alpha=0.7, linewidth=2, label='Hypertension Stage 1')
ax.axhline(y=90, color='#ff6b6b', linestyle='--', alpha=0.7, linewidth=2)
ax.axvline(x=130, color='#ffa502', linestyle=':', alpha=0.6, linewidth=1.5, label='Elevated')
# Normal zone
from matplotlib.patches import Rectangle
normal = Rectangle((90, 55), 30, 25, fill=False, edgecolor='#4ecdc4',
linewidth=2.5, linestyle='-', alpha=0.8, label='Normal')
ax.add_patch(normal)
# Styled colorbar
cbar = plt.colorbar(h[3], ax=ax, pad=0.02, shrink=0.85)
cbar.ax.set_facecolor('#080810')
cbar.set_label('Patient Count', fontsize=11, color='#a0e0e8', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#a0e0e8')
cbar.outline.set_edgecolor('#2a4a58')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#a0e0e8', fontsize=9)
ax.set_xlabel('Systolic (mmHg)', fontsize=13, color='#a0e0e8', fontweight='600', labelpad=10)
ax.set_ylabel('Diastolic (mmHg)', fontsize=13, color='#a0e0e8', fontweight='600', labelpad=10)
ax.set_title('Blood Pressure Distribution', fontsize=16, color='white', fontweight='bold', pad=20)
ax.tick_params(colors='#80c0c8', labelsize=10, length=0)
for spine in ax.spines.values():
spine.set_visible(False)
ax.legend(loc='upper left', fontsize=9, facecolor='#1a2a38', edgecolor='#2a4a58', labelcolor='#a0e0e8')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More 2D Histogram examples
☕