{"spec_id":"qq-basic","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nqq-basic: Basic Q-Q Plot\nLibrary: letsplot 4.11.0 | Python 3.13.14\nQuality: 91/100 | Updated: 2026-07-24\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import (\n    LetsPlot,\n    aes,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_qq,\n    geom_qq_line,\n    geom_text,\n    ggplot,\n    ggsize,\n    labs,\n    theme,\n    theme_minimal,\n)\nfrom lets_plot.export import ggsave\nfrom scipy import stats\n\n\nLetsPlot.setup_html()\n\n# Theme tokens (see prompts/default-style-guide.md \"Theme-adaptive Chrome\")\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nRULE = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\nBRAND = \"#009E73\"  # Imprint palette position 1\n\n# Data - pressure readings from a manufacturing QC calibration line, heavy-tailed\n# (Student's t, df=3) so both ends of the Q-Q plot bow away from the reference\n# line, the classic heavy-tail signature distinct from a simple skew\nnp.random.seed(42)\npressure_psi = stats.t.rvs(df=3, size=150) * 4 + 100\nreadings = pd.DataFrame({\"pressure_psi\": pressure_psi})\n\n# Callout anchored to the sample's own quantile range so it always lands in\n# the empty upper-left corner, calling out the story the data was built to tell\nn = len(pressure_psi)\ncallout = pd.DataFrame(\n    {\n        \"x\": [stats.norm.ppf(0.5 / n)],\n        \"y\": [readings[\"pressure_psi\"].max()],\n        \"label\": [\"Heavy tails: points bow away\\nfrom the reference line at both ends\"],\n    }\n)\n\n# Plot - lets-plot's geom_qq/geom_qq_line compute theoretical quantiles and\n# the fitted reference line internally against the standard normal. The\n# reference line is dashed and muted so the sample points read as the primary\n# layer, with the fitted line as a secondary guide.\nanyplot_theme = theme(\n    plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    panel_border=element_blank(),\n    panel_grid_major=element_line(color=RULE, size=0.5),\n    panel_grid_minor=element_blank(),\n    axis_title=element_text(color=INK, size=12),\n    axis_text=element_text(color=INK_SOFT, size=10),\n    axis_line=element_line(color=INK_SOFT),\n    plot_title=element_text(color=INK, size=16),\n)\n\nplot = (\n    ggplot(readings, aes(sample=\"pressure_psi\"))\n    + geom_qq_line(color=INK_SOFT, size=1.0, linetype=\"dashed\", alpha=0.8)\n    + geom_qq(color=BRAND, size=2.5, alpha=0.75)\n    + geom_text(\n        aes(x=\"x\", y=\"y\", label=\"label\"), data=callout, color=INK_SOFT, size=3.2, hjust=0, vjust=1, lineheight=1.2\n    )\n    + labs(\n        x=\"Theoretical Quantiles\",\n        y=\"Sample Quantiles (Pressure, psi)\",\n        title=\"qq-basic · python · letsplot · anyplot.ai\",\n    )\n    + ggsize(800, 450)\n    + theme_minimal()\n    + anyplot_theme\n)\n\n# Save PNG (scale 4x to get 3200 x 1800 px)\nggsave(plot, f\"plot-{THEME}.png\", path=\".\", scale=4)\n\n# Save HTML\nggsave(plot, f\"plot-{THEME}.html\", path=\".\")\n"}