Hexbin Plot
Clinical Trial Response
Drug dosage vs patient response in pharmaceutical research.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
np.random.seed(42)
n_patients = 6000
dosage = np.random.uniform(10, 200, n_patients)
optimal_dose = 100
response = 80 * np.exp(-((dosage - optimal_dose) / 50) ** 2)
response += np.random.normal(0, 10, n_patients)
response = np.clip(response, 0, 100)
fig, ax = plt.subplots(figsize=(10, 8), facecolor='#ffffff')
ax.set_facecolor('#fdf4ff')
colors = ['#fdf4ff', '#fae8ff', '#f5d0fe', '#f0abfc', '#e879f9',
'#d946ef', '#c026d3', '#a21caf', '#86198f', '#701a75']
cmap = LinearSegmentedColormap.from_list('fuchsia', colors, N=256)
hb = ax.hexbin(dosage, response, gridsize=35, cmap=cmap, mincnt=1,
edgecolors='white', linewidths=0.3)
ax.axvline(x=100, color='#c026d3', linestyle='-', alpha=0.8, linewidth=2.5, label='Optimal Dose')
ax.axhline(y=60, color='#16a34a', linestyle='--', alpha=0.7, linewidth=2, label='Therapeutic Effect')
dose_curve = np.linspace(10, 200, 100)
response_curve = 80 * np.exp(-((dose_curve - 100) / 50) ** 2)
ax.plot(dose_curve, response_curve, '-', color='#86198f', linewidth=2.5, alpha=0.8, label='Dose-Response Curve')
cbar = plt.colorbar(hb, ax=ax, pad=0.02, shrink=0.85)
cbar.set_label('Patient Count', fontsize=11, color='#701a75', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#86198f')
cbar.outline.set_edgecolor('#f5d0fe')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#86198f', fontsize=9)
ax.set_xlabel('Dosage (mg)', fontsize=12, color='#701a75', fontweight='600', labelpad=12)
ax.set_ylabel('Response Score', fontsize=12, color='#701a75', fontweight='600', labelpad=12)
ax.set_title('Clinical Trial Dose-Response', fontsize=16, color='#4a044e', fontweight='700', pad=20)
ax.tick_params(colors='#86198f', labelsize=10, length=0)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#f5d0fe')
ax.spines['bottom'].set_color('#f5d0fe')
ax.legend(loc='upper right', fontsize=9, frameon=True, facecolor='white',
edgecolor='#f5d0fe', labelcolor='#701a75')
ax.grid(True, alpha=0.3, color='#f5d0fe', linestyle='-', linewidth=0.5)
ax.set_axisbelow(True)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Hexbin Plot examples
☕