Linear Regression Plot

Gene Expression Dose-Response Curve

Pharmacogenomics regression with R² annotation and confidence band

Output
Gene Expression Dose-Response Curve
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(42)

# Data
concentration = np.linspace(0.1, 10, 60)
expression = 2.5 * np.log(concentration + 1) + np.random.normal(0, 0.35, 60)

# Regression
slope, intercept, r_value, p_value, std_err = stats.linregress(np.log(concentration + 1), expression)
x_fit = np.linspace(0.1, 10, 100)
y_fit = slope * np.log(x_fit + 1) + intercept

# CI band
y_pred = slope * np.log(concentration + 1) + intercept
residuals = expression - y_pred
se = np.sqrt(np.sum(residuals**2) / (len(concentration) - 2))
ci = 1.96 * se

fig, ax = plt.subplots(figsize=(10, 7), facecolor='#0a0a0f')
ax.set_facecolor('#0a0a0f')

# Confidence band
ax.fill_between(x_fit, slope * np.log(x_fit + 1) + intercept - ci, 
                slope * np.log(x_fit + 1) + intercept + ci,
                color='#27D3F5', alpha=0.15, linewidth=0)

# Regression line
ax.plot(x_fit, y_fit, color='#27D3F5', linewidth=2.5, zorder=3)

# Scatter
ax.scatter(concentration, expression, c='#F5276C', s=70, alpha=0.85, 
           edgecolors='white', linewidths=0.5, zorder=4)

# Stats annotation
stats_text = f'R² = {r_value**2:.3f}\np < 0.001'
ax.text(0.97, 0.05, stats_text, transform=ax.transAxes, fontsize=11,
        color='#888888', ha='right', va='bottom', family='monospace',
        bbox=dict(boxstyle='round,pad=0.4', facecolor='#0a0a0f', edgecolor='#333333', alpha=0.9))

# Minimal spines
for spine in ['top', 'right']:
    ax.spines[spine].set_visible(False)
for spine in ['bottom', 'left']:
    ax.spines[spine].set_color('#333333')
    ax.spines[spine].set_linewidth(0.8)

ax.set_xlabel('Drug Concentration (μM)', fontsize=12, color='white', fontweight='500', labelpad=10)
ax.set_ylabel('Gene Expression (log₂ FC)', fontsize=12, color='white', fontweight='500', labelpad=10)
ax.set_title('Dose-Response Relationship', fontsize=15, color='white', fontweight='bold', pad=20, loc='left')
ax.tick_params(colors='#666666', labelsize=10, length=0)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Pairwise Data

Did this help you?

Support PyLucid to keep it free & growing

Support