Linear Regression Plot
Drug Pharmacokinetics
Two-compartment model with half-life annotation
Output
Python
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(2222)
# PK data - bi-exponential decay
time = np.linspace(0, 24, 55)
A, B = 80, 20 # coefficients
alpha, beta = 1.5, 0.15 # rate constants
conc = A * np.exp(-alpha * time) + B * np.exp(-beta * time) + np.random.normal(0, 3, 55)
conc = np.clip(conc, 0.5, 120)
# Theoretical curve
t_smooth = np.linspace(0, 24, 200)
conc_smooth = A * np.exp(-alpha * t_smooth) + B * np.exp(-beta * t_smooth)
fig, ax = plt.subplots(figsize=(10, 7), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
ax.yaxis.grid(True, color='#f0f0f0', linewidth=1, zorder=1)
ax.xaxis.grid(True, color='#f0f0f0', linewidth=1, zorder=1)
# Phase annotations
ax.axvspan(0, 2, alpha=0.05, color='#F54927', zorder=1)
ax.text(1, 90, 'α phase', fontsize=9, color='#F54927', ha='center')
ax.text(12, 30, 'β phase', fontsize=9, color='#666666', ha='center')
ax.fill_between(t_smooth, conc_smooth * 0.85, conc_smooth * 1.15, color='#D3F527', alpha=0.15, linewidth=0, zorder=2)
ax.plot(t_smooth, conc_smooth, color='#5314E6', linewidth=2.5, zorder=3)
ax.scatter(time, conc, c='#F5B027', s=55, alpha=0.85, edgecolors='white', linewidths=0.6, zorder=4)
# Half-life markers
t_half = np.log(2) / beta
ax.axvline(x=t_half, color='#C82909', linestyle='--', linewidth=1.2, alpha=0.6)
ax.text(t_half + 0.3, 70, f't½ = {t_half:.1f}h', fontsize=10, color='#C82909')
for spine in ['top', 'right']:
ax.spines[spine].set_visible(False)
for spine in ['bottom', 'left']:
ax.spines[spine].set_color('#cccccc')
ax.set_xlabel('Time (hours)', fontsize=12, color='#333333', fontweight='500', labelpad=10)
ax.set_ylabel('Plasma Concentration (ng/mL)', fontsize=12, color='#333333', fontweight='500', labelpad=10)
ax.set_title('Drug Elimination Profile', fontsize=15, color='#1a1a1a', fontweight='bold', pad=20, loc='left')
ax.tick_params(colors='#666666', labelsize=10, length=0)
ax.set_yscale('log')
ax.set_ylim(1, 150)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Pairwise Data
More Linear Regression Plot examples
☕