{"spec_id":"scatter-annotated","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nscatter-annotated: Annotated Scatter Plot with Text Labels\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-13\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom adjustText import adjust_text\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\"\nBRAND = \"#009E73\"  # Okabe-Ito position 1 — ALWAYS first series\n\n# Set seaborn theme with adaptive colors\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Data: Startup companies with revenue vs operating margin\nnp.random.seed(42)\nstartups = [\n    \"TechVenture\",\n    \"DataFlow\",\n    \"CloudScale\",\n    \"NeuralAI\",\n    \"SecureNet\",\n    \"EdgeCompute\",\n    \"ApiPlatform\",\n    \"DevTools\",\n    \"QuantumOps\",\n    \"ByteShift\",\n    \"StreamHub\",\n    \"AutoScale\",\n    \"MetaSync\",\n    \"SignalLabs\",\n    \"FusionCore\",\n]\nn_points = len(startups)\n\n# Revenue (millions) and Operating Margin (%)\nrevenue = np.random.uniform(10, 150, n_points)\noperating_margin = np.random.uniform(5, 35, n_points) + 0.1 * revenue + np.random.randn(n_points) * 5\noperating_margin = np.clip(operating_margin, -20, 45)\n\n# Create DataFrame\ndf = pd.DataFrame({\"company\": startups, \"revenue\": revenue, \"operating_margin\": operating_margin})\n\n# Create figure and plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\n\n# Scatter plot with Okabe-Ito brand color\nsns.scatterplot(\n    data=df, x=\"revenue\", y=\"operating_margin\", s=250, alpha=0.7, color=BRAND, edgecolor=PAGE_BG, linewidth=1.5, ax=ax\n)\n\n# Create text annotations with initial offset (collect for adjustText)\ntexts = []\nfor _, row in df.iterrows():\n    offset_x = 4.0\n    offset_y = 1.5\n    text = ax.text(\n        row[\"revenue\"] + offset_x,\n        row[\"operating_margin\"] + offset_y,\n        row[\"company\"],\n        fontsize=14,\n        color=INK_SOFT,\n        ha=\"left\",\n        va=\"bottom\",\n    )\n    texts.append(text)\n\n# Use adjustText to prevent label overlaps with connecting lines\nadjust_text(\n    texts,\n    x=df[\"revenue\"].values,\n    y=df[\"operating_margin\"].values,\n    arrowprops={\"arrowstyle\": \"-\", \"color\": INK_SOFT, \"alpha\": 0.5, \"lw\": 0.8},\n    expand=(1.3, 1.3),\n    force_text=(0.3, 0.3),\n    force_points=(0.3, 0.3),\n    ax=ax,\n)\n\n# Labels and styling\nax.set_xlabel(\"Annual Revenue ($ Million)\", fontsize=20, color=INK)\nax.set_ylabel(\"Operating Margin (%)\", fontsize=20, color=INK)\nax.set_title(\"scatter-annotated · seaborn · anyplot.ai\", fontsize=24, color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Remove top and right spines\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"left\"].set_color(INK_SOFT)\nax.spines[\"bottom\"].set_color(INK_SOFT)\n\n# Grid on y-axis only\nax.yaxis.grid(True, alpha=0.1, linewidth=0.8, linestyle=\"-\")\nax.xaxis.grid(False)\n\n# Adjust axis limits to accommodate labels\nax.set_xlim(-10, max(revenue) + 25)\nax.set_ylim(min(operating_margin) - 8, max(operating_margin) + 10)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}