|
| 1 | +""" pyplots.ai |
| 2 | +residual-plot: Residual Plot |
| 3 | +Library: altair 6.0.0 | Python 3.13.11 |
| 4 | +Quality: 91/100 | Created: 2025-12-26 |
| 5 | +""" |
| 6 | + |
| 7 | +import altair as alt |
| 8 | +import numpy as np |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | + |
| 12 | +# Data: Simulate a linear regression scenario with some non-linearity |
| 13 | +np.random.seed(42) |
| 14 | +n = 150 |
| 15 | + |
| 16 | +# Generate realistic housing price prediction scenario |
| 17 | +x = np.linspace(1000, 3000, n) # House size in sq ft |
| 18 | +noise = np.random.randn(n) * 15000 |
| 19 | +y_true = 50000 + 150 * x + 0.02 * (x - 2000) ** 2 + noise # True prices with slight curvature |
| 20 | +y_pred = 50000 + 155 * x # Linear model predictions |
| 21 | + |
| 22 | +residuals = y_true - y_pred |
| 23 | +std_residual = np.std(residuals) |
| 24 | + |
| 25 | +# Identify outliers (beyond ±2 standard deviations) |
| 26 | +is_outlier = np.abs(residuals) > 2 * std_residual |
| 27 | + |
| 28 | +# Create DataFrame |
| 29 | +df = pd.DataFrame( |
| 30 | + { |
| 31 | + "Fitted Values ($)": y_pred, |
| 32 | + "Residuals ($)": residuals, |
| 33 | + "Outlier": np.where(is_outlier, "Outlier (>2σ)", "Normal"), |
| 34 | + } |
| 35 | +) |
| 36 | + |
| 37 | +# Base scatter plot with color encoding for outliers |
| 38 | +scatter = ( |
| 39 | + alt.Chart(df) |
| 40 | + .mark_point(size=120, opacity=0.7) |
| 41 | + .encode( |
| 42 | + x=alt.X("Fitted Values ($):Q", title="Fitted Values ($)", scale=alt.Scale(nice=True)), |
| 43 | + y=alt.Y("Residuals ($):Q", title="Residuals ($)", scale=alt.Scale(nice=True)), |
| 44 | + color=alt.Color( |
| 45 | + "Outlier:N", |
| 46 | + scale=alt.Scale(domain=["Normal", "Outlier (>2σ)"], range=["#306998", "#FFD43B"]), |
| 47 | + legend=alt.Legend(title="Point Type", titleFontSize=18, labelFontSize=16), |
| 48 | + ), |
| 49 | + tooltip=["Fitted Values ($):Q", "Residuals ($):Q", "Outlier:N"], |
| 50 | + ) |
| 51 | +) |
| 52 | + |
| 53 | +# Zero reference line |
| 54 | +zero_line = ( |
| 55 | + alt.Chart(pd.DataFrame({"y": [0]})).mark_rule(color="#333333", strokeWidth=2, strokeDash=[8, 4]).encode(y="y:Q") |
| 56 | +) |
| 57 | + |
| 58 | +# ±2 standard deviation bands |
| 59 | +bands_df = pd.DataFrame({"y": [2 * std_residual, -2 * std_residual], "label": ["+2σ", "-2σ"]}) |
| 60 | + |
| 61 | +band_lines = alt.Chart(bands_df).mark_rule(color="#888888", strokeWidth=1.5, strokeDash=[4, 4]).encode(y="y:Q") |
| 62 | + |
| 63 | +# Add LOWESS-like trend using polynomial regression |
| 64 | +loess_df = df.copy() |
| 65 | +loess_df = loess_df.sort_values("Fitted Values ($)") |
| 66 | + |
| 67 | +loess_line = ( |
| 68 | + alt.Chart(loess_df) |
| 69 | + .transform_loess("Fitted Values ($)", "Residuals ($)", bandwidth=0.3) |
| 70 | + .mark_line(color="#E24A33", strokeWidth=3) |
| 71 | + .encode(x="Fitted Values ($):Q", y="Residuals ($):Q") |
| 72 | +) |
| 73 | + |
| 74 | +# Combine all layers |
| 75 | +chart = ( |
| 76 | + alt.layer(zero_line, band_lines, scatter, loess_line) |
| 77 | + .properties( |
| 78 | + width=1600, |
| 79 | + height=900, |
| 80 | + title=alt.Title(text="residual-plot · altair · pyplots.ai", fontSize=28, anchor="middle"), |
| 81 | + ) |
| 82 | + .configure_axis(labelFontSize=18, titleFontSize=22, gridOpacity=0.3) |
| 83 | + .configure_view(strokeWidth=0) |
| 84 | + .configure_legend(orient="right", padding=10) |
| 85 | +) |
| 86 | + |
| 87 | +# Save outputs |
| 88 | +chart.save("plot.png", scale_factor=3.0) |
| 89 | +chart.save("plot.html") |
0 commit comments