{"spec_id":"network-bipartite","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nnetwork-bipartite: Bipartite Network Graph\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 84/100 | Created: 2026-05-14\n\"\"\"\n\nimport os\nimport sys\n\n\n# Workaround for module/filename conflict: remove current dir from path persistently\nsys.path = [p for p in sys.path if not p.startswith(os.path.dirname(os.path.abspath(__file__)))]\n\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_point,\n    geom_segment,\n    geom_text,\n    ggplot,\n    labs,\n    scale_color_manual,\n    scale_size_continuous,\n    scale_x_continuous,\n    scale_y_continuous,\n    theme,\n)\n\n\nnp.random.seed(42)\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\"\n\nCOLOR_GENE = \"#009E73\"  # Okabe-Ito position 1\nCOLOR_DISEASE = \"#4467A3\"  # Okabe-Ito position 3\n\n# Data: gene–disease association network in bioinformatics\ngenes = [\"BRCA1\", \"TP53\", \"PTEN\", \"EGFR\", \"KRAS\", \"PIK3CA\", \"APC\", \"RB1\", \"VHL\", \"CDKN2A\", \"MLH1\", \"ERBB2\"]\ndiseases = [\n    \"Breast Cancer\",\n    \"Lung Cancer\",\n    \"Colorectal Cancer\",\n    \"Prostate Cancer\",\n    \"Glioblastoma\",\n    \"Melanoma\",\n    \"Ovarian Cancer\",\n    \"Leukemia\",\n]\n\n# (gene_index, disease_index) edges\nconnections = [\n    (0, 0),\n    (0, 6),\n    (1, 0),\n    (1, 1),\n    (1, 2),\n    (1, 4),\n    (2, 0),\n    (2, 3),\n    (3, 1),\n    (3, 4),\n    (4, 1),\n    (4, 2),\n    (5, 0),\n    (5, 2),\n    (5, 3),\n    (6, 2),\n    (7, 0),\n    (7, 7),\n    (8, 5),\n    (9, 1),\n    (9, 5),\n    (10, 2),\n    (10, 7),\n    (11, 0),\n    (11, 1),\n]\n\ngene_y = np.linspace(0.05, 0.95, len(genes))\ndisease_y = np.linspace(0.10, 0.90, len(diseases))\n\ngene_df = pd.DataFrame(\n    {\n        \"label\": genes,\n        \"x\": 0.0,\n        \"y\": gene_y,\n        \"node_set\": \"Gene\",\n        \"degree\": [sum(1 for g, _ in connections if g == i) for i in range(len(genes))],\n    }\n)\ndisease_df = pd.DataFrame(\n    {\n        \"label\": diseases,\n        \"x\": 1.0,\n        \"y\": disease_y,\n        \"node_set\": \"Disease\",\n        \"degree\": [sum(1 for _, d in connections if d == i) for i in range(len(diseases))],\n    }\n)\nnodes = pd.concat([gene_df, disease_df], ignore_index=True)\n\nedges = pd.DataFrame(\n    {\n        \"x\": [gene_df.iloc[g][\"x\"] for g, _ in connections],\n        \"y\": [gene_df.iloc[g][\"y\"] for g, _ in connections],\n        \"xend\": [disease_df.iloc[d][\"x\"] for _, d in connections],\n        \"yend\": [disease_df.iloc[d][\"y\"] for _, d in connections],\n    }\n)\n\n# Plot\nplot = (\n    ggplot()\n    + geom_segment(aes(x=\"x\", y=\"y\", xend=\"xend\", yend=\"yend\"), data=edges, color=INK_SOFT, alpha=0.30, size=0.6)\n    + geom_point(aes(x=\"x\", y=\"y\", color=\"node_set\", size=\"degree\"), data=nodes)\n    + geom_text(aes(x=\"x\", y=\"y\", label=\"label\", color=\"node_set\"), data=gene_df, ha=\"right\", nudge_x=-0.04, size=10)\n    + geom_text(aes(x=\"x\", y=\"y\", label=\"label\", color=\"node_set\"), data=disease_df, ha=\"left\", nudge_x=0.04, size=10)\n    + scale_color_manual(values={\"Gene\": COLOR_GENE, \"Disease\": COLOR_DISEASE}, name=\"Node Type\")\n    + scale_size_continuous(range=(4, 14), name=\"Degree\")\n    + scale_x_continuous(limits=(-0.55, 1.55), breaks=[0, 1], labels=[\"Genes\", \"Diseases\"])\n    + scale_y_continuous(limits=(-0.05, 1.05))\n    + labs(title=\"network-bipartite · plotnine · anyplot.ai\", x=\"\", y=\"\")\n    + theme(\n        figure_size=(16, 9),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major=element_blank(),\n        panel_grid_minor=element_blank(),\n        panel_border=element_blank(),\n        axis_title=element_blank(),\n        axis_text_x=element_text(color=INK, size=22, face=\"bold\"),\n        axis_text_y=element_blank(),\n        axis_ticks_major_y=element_blank(),\n        axis_ticks_minor_y=element_blank(),\n        axis_line_x=element_line(color=INK_SOFT, size=0.5),\n        plot_title=element_text(color=INK, size=24),\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\nplot.save(f\"plot-{THEME}.png\", dpi=300, width=16, height=9)\n"}