{"spec_id":"scatter-annotated","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nscatter-annotated: Annotated Scatter Plot with Text Labels\nLibrary: letsplot 4.9.0 | Python 3.13.13\nQuality: 87/100 | Updated: 2026-05-13\n\"\"\"\n\nimport os\nfrom pathlib import Path\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import *\n\nLetsPlot.setup_html()\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\"\nBRAND = \"#009E73\"  # Okabe-Ito position 1\n\n# Data - Company performance metrics (neutral business context)\nnp.random.seed(42)\n\ncompanies = [\n    \"Acme Corp\",\n    \"TechFlow\",\n    \"DataSys\",\n    \"CloudNet\",\n    \"NeuraTech\",\n    \"ByteWorks\",\n    \"InfoPlex\",\n    \"CyberDyn\",\n    \"QuantumAI\",\n    \"LogiCore\",\n    \"MegaSoft\",\n    \"SynergyX\",\n    \"DigiHub\",\n    \"SmartScale\",\n    \"CoreLogic\",\n]\n\n# Revenue (millions) and profit margin (percentage)\nrevenue = np.random.uniform(50, 500, len(companies))\nprofit_margin = np.random.uniform(5, 35, len(companies))\n\n# Add some outliers for visual interest\nrevenue[4] = 480  # NeuraTech - high revenue\nprofit_margin[4] = 32  # NeuraTech - strong margins\nrevenue[8] = 120  # QuantumAI - low revenue\nprofit_margin[8] = 28  # QuantumAI - good margins (efficient startup)\nrevenue[10] = 520  # MegaSoft - highest revenue\nprofit_margin[10] = 18  # MegaSoft - lower margins (competitive market)\n\ndf = pd.DataFrame({\"company\": companies, \"revenue\": revenue, \"profit_margin\": profit_margin})\n\n# Theme-adaptive styling\nanyplot_theme = theme(\n    plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    panel_background=element_rect(fill=PAGE_BG),\n    panel_grid_major=element_line(color=INK if THEME == \"light\" else INK_SOFT, size=0.3),\n    panel_grid_minor=element_blank(),\n    axis_title=element_text(color=INK, size=20),\n    axis_text=element_text(color=INK_SOFT, size=16),\n    axis_line=element_line(color=INK_SOFT, size=0.5),\n    plot_title=element_text(color=INK, size=24, face=\"bold\"),\n    legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n    legend_text=element_text(color=INK_SOFT, size=16),\n    legend_title=element_text(color=INK, size=16),\n)\n\n# Create annotated scatter plot\nplot = (\n    ggplot(df, aes(x=\"revenue\", y=\"profit_margin\"))\n    + geom_point(size=8, color=BRAND, alpha=0.7)\n    + geom_text(aes(label=\"company\"), size=12, nudge_y=1.5, color=INK_SOFT)\n    + scale_y_continuous(limits=[0, 38])\n    + labs(x=\"Annual Revenue ($ millions)\", y=\"Profit Margin (%)\", title=\"scatter-annotated · letsplot · anyplot.ai\")\n    + theme_minimal()\n    + anyplot_theme\n    + ggsize(1600, 900)\n)\n\n# Save as PNG (scale 3x for 4800x2700 px) and HTML\n_script_dir = Path(__file__).parent\nggsave(plot, f\"plot-{THEME}.png\", path=str(_script_dir), scale=3)\nggsave(plot, f\"plot-{THEME}.html\", path=str(_script_dir))\n"}