{"spec_id":"scatter-regression-linear","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nscatter-regression-linear: Scatter Plot with Linear Regression\nLibrary: seaborn 0.13.2 | Python 3.13.14\nQuality: 94/100 | Updated: 2026-08-05\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\nfrom scipy import stats\n\n\n# Theme tokens (see prompts/default-style-guide.md \"Background\" + \"Theme-adaptive Chrome\")\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nBRAND = \"#009E73\"  # Imprint palette position 1 — ALWAYS first series\n\n# Configure seaborn theme (see prompts/library/seaborn.md \"Theme-adaptive Chrome\")\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.15,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Data - Weekly study hours vs exam score, with realistic positive correlation.\n# (Domain switched from temperature/energy per cross-library diversity audit —\n# altair already covers that pairing; this keeps the same regression shape.)\nnp.random.seed(42)\nn_points = 100\nstudy_hours = np.random.uniform(2, 20, n_points)\nexam_score = 38 + 2.9 * study_hours + np.random.normal(0, 8, n_points)\nexam_score = np.clip(exam_score, 30, 100)\n\n# Regression statistics for the annotation (sns.regplot draws the fit + CI band itself)\nslope, intercept, r_value, p_value, std_err = stats.linregress(study_hours, exam_score)\nr_squared = r_value**2\n\n# Create figure and axis — canonical landscape canvas (see prompts/library/seaborn.md \"Canvas\")\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400)\n\n# Idiomatic seaborn regression plot: scatter + linear fit + 95% CI band in one call\nsns.regplot(\n    x=study_hours,\n    y=exam_score,\n    ax=ax,\n    ci=95,\n    scatter_kws={\"s\": 60, \"alpha\": 0.6, \"color\": BRAND, \"edgecolor\": \"white\", \"linewidths\": 0.5},\n    line_kws={\"color\": INK_SOFT, \"linewidth\": 2},\n)\n# regplot fills the CI band with the line color by default — recolor to brand teal\n# so it reads as \"uncertainty around the data\" rather than \"uncertainty around the line\".\nax.collections[-1].set_facecolor(BRAND)\nax.collections[-1].set_alpha(0.15)\n\n# Marginal rug plot — seaborn-native touch that shows each variable's density along its axis\nsns.rugplot(x=study_hours, ax=ax, color=INK_SOFT, alpha=0.3, height=0.03)\nsns.rugplot(y=exam_score, ax=ax, color=INK_SOFT, alpha=0.2, height=0.02)\n\n# Regression equation + R² annotation\nequation_text = f\"y = {slope:.2f}x + {intercept:.1f}\\nR² = {r_squared:.3f}\"\nax.annotate(\n    equation_text,\n    xy=(0.04, 0.95),\n    xycoords=\"axes fraction\",\n    fontsize=9,\n    verticalalignment=\"top\",\n    color=INK,\n    bbox={\"boxstyle\": \"round,pad=0.6\", \"facecolor\": ELEVATED_BG, \"alpha\": 0.9, \"edgecolor\": INK_SOFT, \"linewidth\": 0.8},\n)\n\n# Labels and title\nax.set_xlabel(\"Study Hours per Week\", fontsize=10, color=INK)\nax.set_ylabel(\"Exam Score (%)\", fontsize=10, color=INK)\nax.set_title(\n    \"scatter-regression-linear · python · seaborn · anyplot.ai\", fontsize=12, color=INK, fontweight=\"bold\", pad=12\n)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\n\n# Grid — both axes for scatter plots (see default-style-guide.md \"Grid Guidelines\")\nax.grid(True, alpha=0.15, linewidth=0.6, color=INK)\nax.set_axisbelow(True)\n\n# Spines — L-shaped frame\nsns.despine(ax=ax)\n\n# Axis limits with padding\nax.set_xlim(0, 22)\nax.set_ylim(25, 105)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\nplt.close()\n"}