{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Custom node icons\n\nExample of using custom icons to represent nodes with matplotlib.\n\nImages for node icons courtesy of www.materialui.co\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport networkx as nx\nimport PIL\n\n# Image URLs for graph nodes\nicons = {\n    \"router\": \"icons/router_black_144x144.png\",\n    \"switch\": \"icons/switch_black_144x144.png\",\n    \"PC\": \"icons/computer_black_144x144.png\",\n}\n\n# Load images\nimages = {k: PIL.Image.open(fname) for k, fname in icons.items()}\n\n# Generate the computer network graph\nG = nx.Graph()\n\nG.add_node(\"router\", image=images[\"router\"])\nfor i in range(1, 4):\n    G.add_node(f\"switch_{i}\", image=images[\"switch\"])\n    for j in range(1, 4):\n        G.add_node(\"PC_\" + str(i) + \"_\" + str(j), image=images[\"PC\"])\n\nG.add_edge(\"router\", \"switch_1\")\nG.add_edge(\"router\", \"switch_2\")\nG.add_edge(\"router\", \"switch_3\")\nfor u in range(1, 4):\n    for v in range(1, 4):\n        G.add_edge(\"switch_\" + str(u), \"PC_\" + str(u) + \"_\" + str(v))\n\n# Get a reproducible layout and create figure\npos = nx.spring_layout(G, seed=1734289230)\nfig, ax = plt.subplots()\n\n# Note: the min_source/target_margin kwargs only work with FancyArrowPatch objects.\n# Force the use of FancyArrowPatch for edge drawing by setting `arrows=True`,\n# but suppress arrowheads with `arrowstyle=\"-\"`\nnx.draw_networkx_edges(\n    G,\n    pos=pos,\n    ax=ax,\n    arrows=True,\n    arrowstyle=\"-\",\n    min_source_margin=15,\n    min_target_margin=15,\n)\n\n# Transform from data coordinates (scaled between xlim and ylim) to display coordinates\ntr_figure = ax.transData.transform\n# Transform from display to figure coordinates\ntr_axes = fig.transFigure.inverted().transform\n\n# Select the size of the image (relative to the X axis)\nicon_size = (ax.get_xlim()[1] - ax.get_xlim()[0]) * 0.025\nicon_center = icon_size / 2.0\n\n# Add the respective image to each node\nfor n in G.nodes:\n    xf, yf = tr_figure(pos[n])\n    xa, ya = tr_axes((xf, yf))\n    # get overlapped axes and plot icon\n    a = plt.axes([xa - icon_center, ya - icon_center, icon_size, icon_size])\n    a.imshow(G.nodes[n][\"image\"])\n    a.axis(\"off\")\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}