Linear Regression Plot

House Size vs Sale Price

Real estate market analysis

Output
House Size vs Sale Price
Python
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

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

n = 75
sqft = np.random.uniform(800, 4000, n)
price = 50 + 0.15 * sqft + np.random.normal(0, 50, n)

df = pd.DataFrame({'Square Feet': sqft, 'Price ($K)': price})

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

sns.regplot(
    data=df,
    x='Square Feet',
    y='Price ($K)',
    scatter_kws={'color': '#F5D327', 'alpha': 0.7, 's': 55, 'edgecolor': 'white', 'linewidths': 0.5},
    line_kws={'color': '#F5276C', 'linewidth': 2.5},
    ci=95,
    ax=ax
)

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

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

slope = np.polyfit(sqft, price, 1)[0]
ax.text(0.05, 0.85, '$%.0f/sqft' % (slope * 1000), transform=ax.transAxes, fontsize=11,
        color='#F5D327', va='top')

ax.set_xlabel('Square Feet', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Price ($K)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('House Size vs Sale Price', 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