{"spec_id":"scatter-annotated","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nscatter-annotated: Annotated Scatter Plot with Text Labels\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-05-13\n\"\"\"\n\nimport os\n\nimport numpy as np\nfrom plotly import graph_objects as go\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\"\nGRID = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\nBRAND = \"#009E73\"\nBRAND_ACCENT = \"#C475FD\"\n\n# Data - Top tech companies by market cap and revenue\nnp.random.seed(42)\n\ncompanies = [\n    \"Apple\",\n    \"Microsoft\",\n    \"Alphabet\",\n    \"Amazon\",\n    \"Meta\",\n    \"Tesla\",\n    \"Nvidia\",\n    \"Samsung\",\n    \"TSMC\",\n    \"Oracle\",\n    \"Salesforce\",\n    \"Netflix\",\n]\n\n# Market cap (billions USD) - x axis\nmarket_cap = np.array([2800, 2700, 1700, 1500, 900, 700, 1200, 350, 500, 300, 250, 250])\n\n# Annual revenue (billions USD) - y axis\nrevenue = np.array([380, 210, 280, 520, 130, 95, 60, 230, 70, 50, 32, 33])\n\n# Create figure\nfig = go.Figure()\n\n# Identify key companies (top 3 by market cap) for visual emphasis\ntop_companies = sorted(zip(companies, market_cap), key=lambda x: x[1], reverse=True)[:3]\ntop_names = {name for name, _ in top_companies}\n\n# Color data points: key companies get accent color, others get brand\nmarker_colors = [BRAND_ACCENT if company in top_names else BRAND for company in companies]\n\n# Add scatter points with differentiated colors for emphasis\nfig.add_trace(\n    go.Scatter(\n        x=market_cap,\n        y=revenue,\n        mode=\"markers\",\n        marker=dict(\n            size=[25 if c in top_names else 20 for c in companies],\n            color=marker_colors,\n            opacity=0.8,\n            line=dict(width=2.5, color=\"white\" if THEME == \"light\" else PAGE_BG),\n        ),\n        hovertemplate=(\n            \"<b>%{text}</b><br>\"\n            \"Market Cap: $%{x:.0f}B<br>\"\n            \"Annual Revenue: $%{y:.0f}B<br>\"\n            \"Efficiency: %{customdata:.2f}x\"\n            \"<extra></extra>\"\n        ),\n        text=companies,\n        customdata=revenue / market_cap,\n    )\n)\n\n\n# Smart label positioning with collision detection\ndef calculate_label_positions(companies, market_cap, revenue, x_range=(0, 3000), y_range=(0, 600)):\n    \"\"\"Calculate annotation positions with directional distribution.\"\"\"\n    positions = {}\n    directions = [(100, -30), (100, 30), (-100, -30), (-100, 30), (0, 60), (0, -60)]\n\n    for i, company in enumerate(companies):\n        positions[company] = directions[i % len(directions)]\n\n    return positions\n\n\nposition_adjustments = calculate_label_positions(companies, market_cap, revenue)\n\n# Create annotations with enhanced styling\nannotations = []\n\nfor company, cap, rev in zip(companies, market_cap, revenue):\n    ax, ay = position_adjustments.get(company, (0, -40))\n    is_key = company in top_names\n\n    # Enhanced annotation styling with better visual hierarchy\n    annotations.append(\n        dict(\n            x=cap,\n            y=rev,\n            text=f\"<b>{company}</b>\",\n            showarrow=True,\n            arrowhead=2,\n            arrowsize=1.2,\n            arrowwidth=2.5,\n            arrowcolor=BRAND_ACCENT if is_key else BRAND,\n            ax=ax,\n            ay=ay,\n            font=dict(size=19 if is_key else 18, color=INK, family=\"Arial, sans-serif\"),\n            bgcolor=ELEVATED_BG,\n            bordercolor=BRAND_ACCENT if is_key else INK_SOFT,\n            borderwidth=2 if is_key else 1.5,\n            borderpad=5,\n            opacity=0.95,\n        )\n    )\n\n# Update layout with enhanced visual hierarchy and polish\nfig.update_layout(\n    title=dict(\n        text=\"scatter-annotated · plotly · anyplot.ai\",\n        font=dict(size=28, color=INK, family=\"Arial, sans-serif\"),\n        x=0.5,\n        xanchor=\"center\",\n        y=0.98,\n        yanchor=\"top\",\n    ),\n    xaxis=dict(\n        title=dict(text=\"Market Cap (Billion USD)\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        gridcolor=GRID,\n        gridwidth=1,\n        showline=True,\n        linewidth=2.5,\n        linecolor=INK_SOFT,\n        range=[-100, 3100],\n        zeroline=False,\n    ),\n    yaxis=dict(\n        title=dict(text=\"Annual Revenue (Billion USD)\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        gridcolor=GRID,\n        gridwidth=1,\n        showline=True,\n        linewidth=2.5,\n        linecolor=INK_SOFT,\n        range=[-30, 580],\n        zeroline=False,\n    ),\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    annotations=annotations,\n    margin=dict(l=120, r=100, t=120, b=120),\n    showlegend=False,\n    hovermode=\"closest\",\n    font=dict(family=\"Arial, sans-serif\", color=INK),\n)\n\n# Save with theme-suffixed filenames\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}