Linear Regression Plot

Allometric Scaling in Metabolism

Log-log regression showing metabolic rate scaling law

Output
Allometric Scaling in Metabolism
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(321)

# Allometric data (Kleiber's law: metabolism ~ mass^0.75)
mass = np.logspace(0, 4, 60)  # 1g to 10kg
metabolic_rate = 0.1 * mass**0.74 * np.exp(np.random.normal(0, 0.15, 60))

# Log-log regression
log_mass = np.log10(mass)
log_rate = np.log10(metabolic_rate)
slope, intercept, r_value, _, _ = stats.linregress(log_mass, log_rate)

x_fit = np.linspace(0, 4, 100)
y_fit = slope * x_fit + intercept

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

# Confidence band
ax.fill_between(x_fit, y_fit - 0.2, y_fit + 0.2, color='#27F5B0', alpha=0.12, linewidth=0)

ax.plot(x_fit, y_fit, color='#27F5B0', linewidth=2.5, zorder=3)
ax.scatter(log_mass, log_rate, c='#F527B0', s=60, alpha=0.8, edgecolors='white', linewidths=0.4, zorder=4)

# Scaling exponent annotation
ax.text(0.97, 0.08, f'Scaling exponent: {slope:.2f}\n(Kleiber: 0.75)\nR² = {r_value**2:.3f}',
        transform=ax.transAxes, fontsize=10, color='#888888', ha='right', va='bottom',
        family='monospace', bbox=dict(boxstyle='round,pad=0.4', facecolor='#0a0a0f', edgecolor='#333333'))

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

ax.set_xlabel('Log₁₀ Body Mass (g)', fontsize=12, color='white', fontweight='500', labelpad=10)
ax.set_ylabel('Log₁₀ Metabolic Rate (W)', fontsize=12, color='white', fontweight='500', labelpad=10)
ax.set_title('Metabolic Scaling Law', 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