{"spec_id":"calibration-beer-lambert","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\ncalibration-beer-lambert: Beer-Lambert Calibration Curve\nLibrary: plotnine 0.15.5 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-06-03\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_label,\n    geom_point,\n    geom_segment,\n    ggplot,\n    labs,\n    scale_color_manual,\n    scale_fill_manual,\n    scale_shape_manual,\n    scale_x_continuous,\n    scale_y_continuous,\n    stat_smooth,\n    theme,\n    theme_minimal,\n)\nfrom scipy import stats\n\n\n# Theme tokens — Imprint palette, 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\"\n\n# Imprint palette — positions 1 and 5 (green = calibration standards, red = unknown)\nBRAND = \"#009E73\"  # position 1 — always first series\nUNKNOWN_COLOR = \"#AE3030\"  # position 5 — semantic red for \"unknown\" highlight\n\n# Data\nnp.random.seed(42)\nconcentrations = np.array([0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0])\nmolar_absorptivity = 0.045\nabsorbances = molar_absorptivity * concentrations + np.random.normal(0, 0.008, len(concentrations))\nabsorbances = np.clip(absorbances, 0, None)\n\n# Linear regression\nslope, intercept, r_value, p_value, std_err = stats.linregress(concentrations, absorbances)\nr_squared = r_value**2\n\n# Unknown sample — ~10.5 mg/L (differentiated from Letsplot sibling at 7.5 mg/L)\nunknown_absorbance = 0.47\nunknown_concentration = (unknown_absorbance - intercept) / slope\n\ndf_standards = pd.DataFrame(\n    {\"concentration\": concentrations, \"absorbance\": absorbances, \"series\": \"Calibration Standards\"}\n)\n\ndf_unknown = pd.DataFrame(\n    {\"concentration\": [unknown_concentration], \"absorbance\": [unknown_absorbance], \"series\": \"Unknown Sample\"}\n)\n\ndf_all = pd.concat([df_standards, df_unknown], ignore_index=True)\n\n# Dashed projection lines to both axes\ndf_seg_h = pd.DataFrame(\n    {\"x\": [0.0], \"xend\": [unknown_concentration], \"y\": [unknown_absorbance], \"yend\": [unknown_absorbance]}\n)\ndf_seg_v = pd.DataFrame(\n    {\"x\": [unknown_concentration], \"xend\": [unknown_concentration], \"y\": [0.0], \"yend\": [unknown_absorbance]}\n)\n\n# Annotation text\neq_text = f\"y = {slope:.4f}x + {intercept:.4f}\"\nr2_text = f\"R² = {r_squared:.5f}\"\n\ndf_eq = pd.DataFrame({\"x\": [0.5], \"y\": [0.52], \"label\": [eq_text]})\ndf_r2 = pd.DataFrame({\"x\": [0.5], \"y\": [0.42], \"label\": [r2_text]})\n\n# Plot\nplot = (\n    ggplot(df_standards, aes(x=\"concentration\", y=\"absorbance\"))\n    + stat_smooth(method=\"lm\", color=BRAND, fill=BRAND, alpha=0.15, size=1.2, fullrange=True)\n    + geom_segment(\n        df_seg_h,\n        aes(x=\"x\", xend=\"xend\", y=\"y\", yend=\"yend\"),\n        linetype=\"dashed\",\n        color=INK_SOFT,\n        size=0.6,\n        inherit_aes=False,\n    )\n    + geom_segment(\n        df_seg_v,\n        aes(x=\"x\", xend=\"xend\", y=\"y\", yend=\"yend\"),\n        linetype=\"dashed\",\n        color=INK_SOFT,\n        size=0.6,\n        inherit_aes=False,\n    )\n    + geom_point(\n        df_all,\n        aes(x=\"concentration\", y=\"absorbance\", color=\"series\", shape=\"series\", fill=\"series\"),\n        size=4,\n        stroke=1.0,\n        inherit_aes=False,\n    )\n    + scale_color_manual(values={\"Calibration Standards\": BRAND, \"Unknown Sample\": UNKNOWN_COLOR}, name=\" \")\n    + scale_fill_manual(values={\"Calibration Standards\": BRAND, \"Unknown Sample\": UNKNOWN_COLOR}, name=\" \")\n    + scale_shape_manual(values={\"Calibration Standards\": \"o\", \"Unknown Sample\": \"D\"}, name=\" \")\n    + geom_label(\n        df_eq,\n        aes(x=\"x\", y=\"y\", label=\"label\"),\n        ha=\"left\",\n        size=3.5,\n        color=INK,\n        fill=ELEVATED_BG,\n        label_size=0.25,\n        label_r=0.03,\n        inherit_aes=False,\n        show_legend=False,\n    )\n    + geom_label(\n        df_r2,\n        aes(x=\"x\", y=\"y\", label=\"label\"),\n        ha=\"left\",\n        size=3.5,\n        color=INK,\n        fill=ELEVATED_BG,\n        label_size=0.25,\n        label_r=0.03,\n        inherit_aes=False,\n        show_legend=False,\n    )\n    + scale_x_continuous(breaks=np.arange(0, 14, 2), limits=(-0.5, 13.5), expand=(0, 0.3))\n    + scale_y_continuous(breaks=np.arange(0, 0.65, 0.1), limits=(-0.02, 0.62), expand=(0, 0.01))\n    + labs(x=\"Concentration (mg/L)\", y=\"Absorbance\", title=\"calibration-beer-lambert · python · plotnine · anyplot.ai\")\n    + theme_minimal()\n    + theme(\n        figure_size=(8, 4.5),\n        text=element_text(size=7, family=\"sans-serif\"),\n        axis_title=element_text(size=10, weight=\"bold\", color=INK),\n        axis_text=element_text(size=8, color=INK_SOFT),\n        plot_title=element_text(size=10, weight=\"bold\", color=INK),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major_x=element_blank(),\n        panel_grid_minor=element_blank(),\n        panel_grid_major_y=element_line(color=INK, size=0.2, alpha=0.15),\n        axis_line_x=element_line(color=INK_SOFT, size=0.4),\n        axis_line_y=element_line(color=INK_SOFT, size=0.4),\n        legend_position=\"bottom\",\n        legend_text=element_text(size=8, color=INK_SOFT),\n        legend_title=element_text(color=INK),\n        legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n        legend_key=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        plot_margin=0.04,\n    )\n)\n\n# Save — canonical 3200×1800 px (8 in × 4.5 in @ dpi=400)\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=8, height=4.5, units=\"in\", verbose=False)\n"}