{"spec_id":"scatter-embedding","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nscatter-embedding: t-SNE and UMAP Embedding Visualization\nLibrary: plotly 6.9.0 | Python 3.13.14\nQuality: 87/100 | Updated: 2026-08-11\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\nfrom sklearn.datasets import make_blobs\nfrom sklearn.manifold import TSNE\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)\"\n\n# Imprint palette in canonical order, except Erythrocytes takes the matte-red\n# anchor (#AE3030) for its blood association (default-style-guide.md \"Semantic\n# exception\") instead of its ordinal position 6.\nCELL_TYPES = [\"T Cells\", \"B Cells\", \"NK Cells\", \"Monocytes\", \"Dendritic Cells\", \"Erythrocytes\"]\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#2ABCCD\", \"#AE3030\"]\n\n# Data — simulate single-cell RNA-seq transcriptomes across 50 marker genes\nnp.random.seed(42)\n\nn_clusters = len(CELL_TYPES)\ngene_expression, labels = make_blobs(\n    n_samples=1500, n_features=50, centers=n_clusters, cluster_std=6.0, center_box=(-30, 30), random_state=42\n)\n\n# Reduce to 2D with t-SNE\ntsne = TSNE(n_components=2, perplexity=30, random_state=42, max_iter=1000)\nembedding_2d = tsne.fit_transform(gene_expression)\n\n# Plot — one trace per cell type\nfig = go.Figure()\n\nfor i, cell_type in enumerate(CELL_TYPES):\n    mask = labels == i\n    fig.add_trace(\n        go.Scatter(\n            x=embedding_2d[mask, 0],\n            y=embedding_2d[mask, 1],\n            mode=\"markers\",\n            name=cell_type,\n            marker={\"color\": IMPRINT[i], \"size\": 9, \"opacity\": 0.65, \"line\": {\"color\": PAGE_BG, \"width\": 0.5}},\n            hovertemplate=\"<b>%{fullData.name}</b><br>t-SNE 1: %{x:.2f}<br>t-SNE 2: %{y:.2f}<extra></extra>\",\n        )\n    )\n\n# Annotate cluster centroids — offset below each cluster's point cloud so the\n# label box sits beneath the markers instead of covering them\ny_margin = 0.06 * (embedding_2d[:, 1].max() - embedding_2d[:, 1].min())\nfor i, cell_type in enumerate(CELL_TYPES):\n    mask = labels == i\n    cx = float(embedding_2d[mask, 0].mean())\n    cy_bottom = float(embedding_2d[mask, 1].min()) - y_margin\n    fig.add_annotation(\n        x=cx,\n        y=cy_bottom,\n        yanchor=\"top\",\n        text=f\"<b>{cell_type}</b>\",\n        showarrow=False,\n        font={\"size\": 17, \"color\": INK},\n        bgcolor=ELEVATED_BG,\n        bordercolor=INK_SOFT,\n        borderwidth=2,\n        borderpad=4,\n        opacity=1.0,\n    )\n\n# Layout\ntitle_text = \"Single-Cell RNA-Seq Clustering · scatter-embedding · python · plotly · anyplot.ai\"\nfig.update_layout(\n    autosize=False,\n    width=800,\n    height=450,\n    title={\n        \"text\": (\n            f\"<b>{title_text}</b>\"\n            \"<br><sup>t-SNE (perplexity=30) | 1,500 synthetic transcriptomes across 6 cell types</sup>\"\n        ),\n        \"font\": {\"size\": 13, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font={\"color\": INK},\n    xaxis={\n        \"title\": {\"text\": \"t-SNE Dimension 1\", \"font\": {\"size\": 12, \"color\": INK}},\n        \"showticklabels\": False,\n        \"showgrid\": True,\n        \"gridcolor\": GRID,\n        \"showline\": False,\n        \"zeroline\": False,\n    },\n    yaxis={\n        \"title\": {\"text\": \"t-SNE Dimension 2\", \"font\": {\"size\": 12, \"color\": INK}},\n        \"showticklabels\": False,\n        \"showgrid\": True,\n        \"gridcolor\": GRID,\n        \"showline\": False,\n        \"zeroline\": False,\n    },\n    legend={\n        \"bgcolor\": ELEVATED_BG,\n        \"bordercolor\": INK_SOFT,\n        \"borderwidth\": 1,\n        \"font\": {\"size\": 16, \"color\": INK_SOFT},\n        \"title\": {\"text\": \"Cell Type\", \"font\": {\"size\": 18, \"color\": INK}},\n    },\n    hoverlabel={\"bgcolor\": ELEVATED_BG, \"bordercolor\": INK_SOFT, \"font\": {\"color\": INK, \"size\": 14}},\n    margin={\"l\": 80, \"r\": 80, \"t\": 120, \"b\": 80},\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=800, height=450, scale=4)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}