Linear Regression Plot

Engine Size vs Fuel Economy

Automotive efficiency analysis

Output
Engine Size vs Fuel Economy
Python
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

np.random.seed(409)
BG_COLOR = '#0a0a0f'
TEXT_COLOR = 'white'

n = 80
engine = np.random.uniform(1.0, 6.0, n)
mpg = 45 - 5 * engine + np.random.normal(0, 4, n)

df = pd.DataFrame({'Engine Size (L)': engine, 'MPG': mpg})

fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)

sns.regplot(
    data=df,
    x='Engine Size (L)',
    y='MPG',
    scatter_kws={'color': '#F54927', 'alpha': 0.7, 's': 55, 'edgecolor': 'white', 'linewidths': 0.5},
    line_kws={'color': '#6CF527', 'linewidth': 2.5},
    ci=95,
    ax=ax
)

for collection in ax.collections[1:]:
    collection.set_facecolor('#6CF527')
    collection.set_alpha(0.15)

corr = np.corrcoef(engine, mpg)[0, 1]
ax.text(0.95, 0.95, 'r = %.3f' % corr, transform=ax.transAxes, fontsize=12,
        color='#6CF527', fontweight='bold', va='top', ha='right',
        bbox=dict(boxstyle='round,pad=0.3', facecolor='#1a1a2e', edgecolor='#333'))

ax.set_xlabel('Engine Size (Liters)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Fuel Economy (MPG)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Engine Size vs Fuel Economy', fontsize=14, color=TEXT_COLOR, fontweight='bold', pad=15)

ax.tick_params(colors='#888', labelsize=10)
for spine in ax.spines.values():
    spine.set_color('#333')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Pairwise Data

Did this help you?

Support PyLucid to keep it free & growing

Support