Linear Regression Plot

Temperature vs Ice Cream Sales

Weather impact on consumer behavior

Output
Temperature vs Ice Cream Sales
Python
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

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

n = 60
temp = np.random.uniform(50, 100, n)
sales = 0.8 * temp + np.random.normal(0, 8, n) - 20

df = pd.DataFrame({'Temperature (°F)': temp, 'Sales ($K)': sales})

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

sns.regplot(
    data=df,
    x='Temperature (°F)',
    y='Sales ($K)',
    scatter_kws={'color': '#27D3F5', 'alpha': 0.7, 's': 60, 'edgecolor': 'white', 'linewidths': 0.5},
    line_kws={'color': '#F54927', 'linewidth': 2.5},
    ci=95,
    ax=ax
)

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

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

ax.set_xlabel('Temperature (°F)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Daily Sales ($K)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Temperature vs Ice Cream Sales', 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