Linear Regression Plot

Stellar Mass-Luminosity Relation

Astrophysics power law regression for main sequence stars

Output
Stellar Mass-Luminosity Relation
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(111)

# Stellar data (L ~ M^3.5 for main sequence)
mass = np.logspace(-0.5, 1.3, 55)  # 0.3 to 20 solar masses
luminosity = mass**3.5 * np.exp(np.random.normal(0, 0.25, 55))

log_mass = np.log10(mass)
log_lum = np.log10(luminosity)
slope, intercept, r_value, _, _ = stats.linregress(log_mass, log_lum)

x_fit = np.linspace(-0.5, 1.3, 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.3, y_fit + 0.3, color='#F5D327', alpha=0.12, linewidth=0)
ax.plot(x_fit, y_fit, color='#F5D327', linewidth=2.5, zorder=3)
ax.scatter(log_mass, log_lum, c='#27D3F5', s=60, alpha=0.85, edgecolors='white', linewidths=0.4, zorder=4)

# Sun marker
ax.scatter([0], [0], c='#F5B027', s=150, marker='*', zorder=5, edgecolors='white', linewidths=1)
ax.text(0.08, 0.3, '☉', fontsize=14, color='#F5B027')

ax.text(0.97, 0.05, f'L ∝ M^{slope:.2f}\nR² = {r_value**2:.3f}',
        transform=ax.transAxes, fontsize=10, color='#888888', ha='right', va='bottom', 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₁₀ Stellar Mass (M☉)', fontsize=12, color='white', fontweight='500', labelpad=10)
ax.set_ylabel('Log₁₀ Luminosity (L☉)', fontsize=12, color='white', fontweight='500', labelpad=10)
ax.set_title('Main Sequence Stars', 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