{"spec_id":"network-transport-static","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nnetwork-transport-static: Static Transport Network Diagram\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 95/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\nimport sys\nimport time\nfrom pathlib import Path\n\n\n# Remove the current directory from sys.path to avoid circular imports with bokeh.py\nsys.path = [p for p in sys.path if p not in (\"\", \".\", os.getcwd(), os.path.dirname(__file__))]\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import Arrow, ColumnDataSource, Label, LabelSet, VeeHead\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\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\"\n\n# Okabe-Ito palette - first series is always #009E73\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\"]\n\nnp.random.seed(42)\n\n# Data: Regional rail network with stations and routes\n# Scaled coordinates for 4800x2700 canvas with margins\nstations = [\n    {\"id\": \"A\", \"label\": \"Central Station\", \"x\": 500, \"y\": 1350},\n    {\"id\": \"B\", \"label\": \"North Terminal\", \"x\": 1100, \"y\": 2100},\n    {\"id\": \"C\", \"label\": \"East Junction\", \"x\": 1900, \"y\": 1350},\n    {\"id\": \"D\", \"label\": \"South Hub\", \"x\": 1100, \"y\": 600},\n    {\"id\": \"E\", \"label\": \"Airport\", \"x\": 2700, \"y\": 1900},\n    {\"id\": \"F\", \"label\": \"University\", \"x\": 2700, \"y\": 800},\n    {\"id\": \"G\", \"label\": \"Industrial Park\", \"x\": 3600, \"y\": 1350},\n    {\"id\": \"H\", \"label\": \"Harbor\", \"x\": 700, \"y\": 2300},\n    {\"id\": \"I\", \"label\": \"Stadium\", \"x\": 700, \"y\": 400},\n    {\"id\": \"J\", \"label\": \"Tech Campus\", \"x\": 3200, \"y\": 2100},\n    {\"id\": \"K\", \"label\": \"Medical Center\", \"x\": 3200, \"y\": 600},\n    {\"id\": \"L\", \"label\": \"Downtown\", \"x\": 2300, \"y\": 1350},\n]\n\nstation_lookup = {s[\"id\"]: s for s in stations}\n\n# Routes: Train services with departure and arrival times\nroutes = [\n    {\"source_id\": \"A\", \"target_id\": \"B\", \"route_id\": \"RE 10\", \"departure_time\": \"06:00\", \"arrival_time\": \"06:25\"},\n    {\"source_id\": \"A\", \"target_id\": \"D\", \"route_id\": \"RE 20\", \"departure_time\": \"06:15\", \"arrival_time\": \"06:35\"},\n    {\"source_id\": \"A\", \"target_id\": \"C\", \"route_id\": \"RE 30\", \"departure_time\": \"06:30\", \"arrival_time\": \"07:00\"},\n    {\"source_id\": \"B\", \"target_id\": \"H\", \"route_id\": \"S1\", \"departure_time\": \"06:35\", \"arrival_time\": \"06:50\"},\n    {\"source_id\": \"B\", \"target_id\": \"E\", \"route_id\": \"RE 10\", \"departure_time\": \"06:40\", \"arrival_time\": \"07:15\"},\n    {\"source_id\": \"C\", \"target_id\": \"L\", \"route_id\": \"RE 30\", \"departure_time\": \"07:05\", \"arrival_time\": \"07:20\"},\n    {\"source_id\": \"C\", \"target_id\": \"F\", \"route_id\": \"RE 40\", \"departure_time\": \"07:10\", \"arrival_time\": \"07:35\"},\n    {\"source_id\": \"D\", \"target_id\": \"I\", \"route_id\": \"S2\", \"departure_time\": \"06:45\", \"arrival_time\": \"07:00\"},\n    {\"source_id\": \"D\", \"target_id\": \"F\", \"route_id\": \"RE 20\", \"departure_time\": \"06:50\", \"arrival_time\": \"07:25\"},\n    {\"source_id\": \"E\", \"target_id\": \"J\", \"route_id\": \"RE 10\", \"departure_time\": \"07:25\", \"arrival_time\": \"07:40\"},\n    {\"source_id\": \"F\", \"target_id\": \"G\", \"route_id\": \"RE 40\", \"departure_time\": \"07:45\", \"arrival_time\": \"08:10\"},\n    {\"source_id\": \"F\", \"target_id\": \"K\", \"route_id\": \"RE 20\", \"departure_time\": \"07:35\", \"arrival_time\": \"07:55\"},\n    {\"source_id\": \"L\", \"target_id\": \"E\", \"route_id\": \"EX 1\", \"departure_time\": \"07:30\", \"arrival_time\": \"07:50\"},\n    {\"source_id\": \"L\", \"target_id\": \"G\", \"route_id\": \"RE 30\", \"departure_time\": \"07:25\", \"arrival_time\": \"08:00\"},\n    {\"source_id\": \"J\", \"target_id\": \"G\", \"route_id\": \"RE 10\", \"departure_time\": \"07:50\", \"arrival_time\": \"08:15\"},\n    {\"source_id\": \"K\", \"target_id\": \"G\", \"route_id\": \"RE 20\", \"departure_time\": \"08:05\", \"arrival_time\": \"08:25\"},\n    {\"source_id\": \"A\", \"target_id\": \"L\", \"route_id\": \"EX 1\", \"departure_time\": \"07:00\", \"arrival_time\": \"07:20\"},\n    {\"source_id\": \"H\", \"target_id\": \"A\", \"route_id\": \"S1\", \"departure_time\": \"05:30\", \"arrival_time\": \"05:55\"},\n    {\"source_id\": \"I\", \"target_id\": \"A\", \"route_id\": \"S2\", \"departure_time\": \"05:45\", \"arrival_time\": \"06:10\"},\n]\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"network-transport-static · python · bokeh · anyplot.ai\",\n    x_range=(100, 4100),\n    y_range=(100, 2550),\n    tools=\"\",\n    toolbar_location=None,\n)\n\n# Style the figure - scaled for 4800x2700 canvas with theme-adaptive colors\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\np.title.align = \"center\"\np.xgrid.visible = False\np.ygrid.visible = False\np.xaxis.visible = False\np.yaxis.visible = False\np.outline_line_color = None\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.min_border_left = 80\np.min_border_right = 80\np.min_border_top = 100\np.min_border_bottom = 80\n\n# Draw routes as arrows with labels\nfor route in routes:\n    src = station_lookup[route[\"source_id\"]]\n    tgt = station_lookup[route[\"target_id\"]]\n\n    # Calculate direction vector\n    dx = tgt[\"x\"] - src[\"x\"]\n    dy = tgt[\"y\"] - src[\"y\"]\n    length = np.sqrt(dx**2 + dy**2)\n\n    # Normalize direction\n    if length > 0:\n        nx, ny = dx / length, dy / length\n    else:\n        nx, ny = 0, 0\n\n    # Offset for multiple routes between same stations (perpendicular offset)\n    # Find how many routes share this pair\n    pair_key = tuple(sorted([route[\"source_id\"], route[\"target_id\"]]))\n    same_pair_routes = [r for r in routes if tuple(sorted([r[\"source_id\"], r[\"target_id\"]])) == pair_key]\n    pair_index = same_pair_routes.index(route)\n    num_routes = len(same_pair_routes)\n\n    # Perpendicular vector\n    perp_x, perp_y = -ny, nx\n    offset_amount = (pair_index - (num_routes - 1) / 2) * 80\n\n    # Start and end points with offset and shortened to not overlap nodes\n    node_radius = 120\n    start_x = src[\"x\"] + nx * node_radius + perp_x * offset_amount\n    start_y = src[\"y\"] + ny * node_radius + perp_y * offset_amount\n    end_x = tgt[\"x\"] - nx * node_radius + perp_x * offset_amount\n    end_y = tgt[\"y\"] - ny * node_radius + perp_y * offset_amount\n\n    route_id = route[\"route_id\"]\n    if route_id.startswith(\"RE\"):\n        color = IMPRINT[0]\n    elif route_id.startswith(\"S\"):\n        color = IMPRINT[1]\n    elif route_id.startswith(\"EX\"):\n        color = IMPRINT[2]\n    else:\n        color = IMPRINT[3]\n\n    # Draw arrow - scaled for large canvas\n    p.add_layout(\n        Arrow(\n            end=VeeHead(size=35, fill_color=color, line_color=color),\n            x_start=start_x,\n            y_start=start_y,\n            x_end=end_x,\n            y_end=end_y,\n            line_color=color,\n            line_width=6,\n            line_alpha=0.85,\n        )\n    )\n\n    # Route label at midpoint\n    mid_x = (start_x + end_x) / 2 + perp_x * 40\n    mid_y = (start_y + end_y) / 2 + perp_y * 40\n\n    label_text = f\"{route['route_id']} | {route['departure_time']}→{route['arrival_time']}\"\n\n    # Calculate angle for label rotation\n    angle = np.arctan2(dy, dx)\n    if angle > np.pi / 2:\n        angle -= np.pi\n    elif angle < -np.pi / 2:\n        angle += np.pi\n\n    p.add_layout(\n        Label(\n            x=mid_x,\n            y=mid_y,\n            text=label_text,\n            text_font_size=\"18pt\",\n            text_color=color,\n            text_font_style=\"bold\",\n            text_align=\"center\",\n            text_baseline=\"middle\",\n            angle=angle,\n            background_fill_color=ELEVATED_BG,\n            background_fill_alpha=0.9,\n        )\n    )\n\n# Draw station nodes\nstation_x = [s[\"x\"] for s in stations]\nstation_y = [s[\"y\"] for s in stations]\nstation_labels = [s[\"label\"] for s in stations]\n\nstation_source = ColumnDataSource(data={\"x\": station_x, \"y\": station_y, \"label\": station_labels})\n\n# Draw station circles - larger for visibility\np.scatter(x=\"x\", y=\"y\", source=station_source, size=100, fill_color=ELEVATED_BG, line_color=INK_SOFT, line_width=6)\n\n# Station labels - positioned below nodes for clarity\nlabels = LabelSet(\n    x=\"x\",\n    y=\"y\",\n    text=\"label\",\n    source=station_source,\n    text_font_size=\"18pt\",\n    text_font_style=\"bold\",\n    text_color=INK,\n    text_align=\"center\",\n    text_baseline=\"top\",\n    y_offset=-70,\n)\np.add_layout(labels)\n\n# Add legend - positioned and scaled for large canvas\nlegend_x = 3750\nlegend_y = 2400\nlegend_items = [\n    (\"Regional Express (RE)\", IMPRINT[0]),\n    (\"S-Bahn Local (S)\", IMPRINT[1]),\n    (\"Express (EX)\", IMPRINT[2]),\n]\n\nfor i, (label, color) in enumerate(legend_items):\n    y_pos = legend_y - i * 150\n    p.scatter(x=[legend_x], y=[y_pos], size=40, fill_color=color, line_color=color)\n    p.add_layout(\n        Label(x=legend_x + 70, y=y_pos, text=label, text_font_size=\"24pt\", text_color=INK, text_baseline=\"middle\")\n    )\n\n# Save HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome\nW, H = 4800, 2700\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}