{"spec_id":"scatter-annotated","library":"altair","language":"python","code":"\"\"\" anyplot.ai\nscatter-annotated: Annotated Scatter Plot with Text Labels\nLibrary: altair 6.1.0 | Python 3.13.13\nQuality: 88/100 | Updated: 2026-05-13\n\"\"\"\n\nimport os\n\nimport altair as alt\nimport numpy as np\nimport pandas as pd\n\n\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\"\n\n# Data - Selected tech companies showing range of revenue/profit metrics\nnp.random.seed(42)\ncompanies = [\"NVIDIA\", \"Apple\", \"Microsoft\", \"Amazon\", \"Google\", \"Meta\", \"Adobe\", \"Oracle\", \"Tesla\", \"Intel\"]\n# Revenue (billions USD) - realistic values\nrevenue = np.array([61, 385, 211, 574, 307, 135, 19, 50, 97, 63])\n# Profit margin (%) - realistic values\nprofit_margin = np.array([55, 25, 35, 6, 22, 20, 34, 26, 11, 8])\n\ndf = pd.DataFrame({\"company\": companies, \"revenue\": revenue, \"profit_margin\": profit_margin})\n\n# Points layer with Okabe-Ito brand color\npoints = (\n    alt.Chart(df)\n    .mark_point(size=250, filled=True, opacity=0.7, color=BRAND)\n    .encode(\n        x=alt.X(\"revenue:Q\", title=\"Revenue (Billions USD)\", scale=alt.Scale(domain=[0, 620])),\n        y=alt.Y(\"profit_margin:Q\", title=\"Profit Margin (%)\", scale=alt.Scale(domain=[0, 60])),\n        tooltip=[\"company:N\", \"revenue:Q\", \"profit_margin:Q\"],\n    )\n)\n\n# Connector lines from points to labels\nconnector_lines = (\n    alt.Chart(df)\n    .mark_line(size=1, opacity=0.3, color=INK_SOFT)\n    .encode(x=\"revenue:Q\", y=\"profit_margin:Q\", x2=alt.X2(\"revenue:Q\"), y2=alt.Y2(\"profit_margin:Q\"))\n)\n\n# Text labels layer with theme-adaptive color\nlabels = (\n    alt.Chart(df)\n    .mark_text(align=\"left\", dx=12, dy=-8, fontSize=18, fontWeight=\"bold\", color=INK)\n    .encode(x=alt.X(\"revenue:Q\"), y=alt.Y(\"profit_margin:Q\"), text=\"company:N\")\n)\n\n# Combine layers with theme-adaptive styling\nchart = (\n    (points + connector_lines + labels)\n    .properties(\n        width=1600,\n        height=900,\n        background=PAGE_BG,\n        title=alt.Title(text=\"scatter-annotated · altair · anyplot.ai\", fontSize=28, anchor=\"middle\", color=INK),\n    )\n    .configure_view(strokeWidth=0, fill=PAGE_BG)\n    .configure_axis(\n        domainColor=INK_SOFT,\n        tickColor=INK_SOFT,\n        gridColor=INK,\n        gridOpacity=0.10,\n        labelColor=INK_SOFT,\n        labelFontSize=18,\n        titleColor=INK,\n        titleFontSize=22,\n    )\n    .configure_title(color=INK)\n    .configure_legend(fillColor=ELEVATED_BG, strokeColor=INK_SOFT, labelColor=INK_SOFT, titleColor=INK)\n)\n\n# Save as PNG (4800x2700 with scale_factor=3)\nchart.save(f\"plot-{THEME}.png\", scale_factor=3.0)\n\n# Save as HTML for interactivity\nchart.save(f\"plot-{THEME}.html\")\n"}