{"spec_id":"scatter-regression-linear","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nscatter-regression-linear: Scatter Plot with Linear Regression\nLibrary: plotnine 0.15.7 | Python 3.13.14\nQuality: 88/100 | Updated: 2026-08-05\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    annotate,\n    element_line,\n    element_rect,\n    element_text,\n    geom_point,\n    geom_smooth,\n    ggplot,\n    ggsave,\n    labs,\n    theme,\n    theme_minimal,\n)\nfrom scipy import stats\n\n\n# Theme tokens\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\"\n\n# Imprint palette\nBRAND = \"#009E73\"  # Imprint position 1 — scatter points\nACCENT = \"#C475FD\"  # Imprint position 2 — regression line + CI band\n\n# Data - Study hours vs exam score relationship\nnp.random.seed(42)\nn_points = 80\nstudy_hours = np.random.uniform(1, 10, n_points)\nexam_score = 45 + 5 * study_hours + np.random.normal(0, 6, n_points)\nexam_score = np.clip(exam_score, 0, 100)\n\ndf = pd.DataFrame({\"study_hours\": study_hours, \"exam_score\": exam_score})\n\n# Calculate regression statistics for annotation\nslope, intercept, r_value, p_value, std_err = stats.linregress(study_hours, exam_score)\nr_squared = r_value**2\n\nequation_text = f\"y = {slope:.2f}x + {intercept:.2f}\"\nr_squared_text = f\"R² = {r_squared:.3f}\"\nannotation_text = f\"{equation_text}\\n{r_squared_text}\"\n\n# Title — mandated format, length within the 67-char baseline so fontsize stays at default\ntitle = \"scatter-regression-linear · python · plotnine · anyplot.ai\"\ntitle_fontsize = round(12 * (67 / len(title) if len(title) > 67 else 1.0))\n\n# Plot\nplot = (\n    ggplot(df, aes(x=\"study_hours\", y=\"exam_score\"))\n    + geom_point(size=3.0, stroke=0.6, alpha=0.70, fill=BRAND, color=\"white\")\n    + geom_smooth(method=\"lm\", se=True, color=ACCENT, fill=ACCENT, alpha=0.25, size=1.2)\n    + annotate(\n        \"label\",\n        x=2,\n        y=94,\n        label=annotation_text,\n        ha=\"left\",\n        va=\"top\",\n        size=5,\n        fontweight=\"bold\",\n        color=INK,\n        fill=ELEVATED_BG,\n        boxcolor=INK_SOFT,\n        label_padding=0.4,\n    )\n    + labs(title=title, x=\"Study Hours\", y=\"Exam Score (%)\")\n    + theme_minimal()\n    + theme(\n        figure_size=(8, 4.5),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_grid_major=element_line(color=INK, size=0.3, alpha=0.08),\n        panel_grid_minor=element_line(color=INK, size=0.2, alpha=0.04),\n        axis_line=element_line(color=INK_SOFT, size=0.5),\n        axis_title=element_text(size=10, color=INK),\n        axis_text=element_text(size=8, color=INK_SOFT),\n        plot_title=element_text(size=title_fontsize, color=INK, weight=\"bold\"),\n        text=element_text(size=7, color=INK),\n    )\n)\n\n# Save\nggsave(plot, filename=f\"plot-{THEME}.png\", dpi=400, width=8, height=4.5, verbose=False)\n"}