Linear Regression Plot

Species-Area Relationship

Ecological power law with log-transformed axes

Output
Species-Area Relationship
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(987)

# Island biogeography data
area = np.logspace(0, 5, 50)  # 1 to 100,000 km²
z = 0.27  # typical z-value
c = 10
species = c * area**z * np.exp(np.random.normal(0, 0.2, 50))

log_area = np.log10(area)
log_species = np.log10(species)
slope, intercept, r_value, _, _ = stats.linregress(log_area, log_species)

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

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

ax.fill_between(x_fit, y_fit - 0.15, y_fit + 0.15, color='#D3F527', alpha=0.12, linewidth=0)
ax.plot(x_fit, y_fit, color='#D3F527', linewidth=2.5, zorder=3)
ax.scatter(log_area, log_species, c='#4927F5', s=65, alpha=0.85, edgecolors='white', linewidths=0.4, zorder=4)

# Power law annotation
ax.text(0.03, 0.97, f'S = {10**intercept:.1f} × A^{slope:.2f}\nz = {slope:.2f}\nR² = {r_value**2:.3f}',
        transform=ax.transAxes, fontsize=10, color='#aaaaaa', ha='left', va='top', family='monospace')

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₁₀ Island Area (km²)', fontsize=12, color='white', fontweight='500', labelpad=10)
ax.set_ylabel('Log₁₀ Species Richness', fontsize=12, color='white', fontweight='500', labelpad=10)
ax.set_title('Island Biogeography', 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