dwww Home | Show directory contents | Find package

{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# DAG - Topological Layout\n\nThis example combines the `topological_generations` generator with\n`multipartite_layout` to show how to visualize a DAG in topologically-sorted\norder.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import networkx as nx\nimport matplotlib.pyplot as plt\n\n\nG = nx.DiGraph(\n    [\n        (\"f\", \"a\"),\n        (\"a\", \"b\"),\n        (\"a\", \"e\"),\n        (\"b\", \"c\"),\n        (\"b\", \"d\"),\n        (\"d\", \"e\"),\n        (\"f\", \"c\"),\n        (\"f\", \"g\"),\n        (\"h\", \"f\"),\n    ]\n)\n\nfor layer, nodes in enumerate(nx.topological_generations(G)):\n    # `multipartite_layout` expects the layer as a node attribute, so add the\n    # numeric layer value as a node attribute\n    for node in nodes:\n        G.nodes[node][\"layer\"] = layer\n\n# Compute the multipartite_layout using the \"layer\" node attribute\npos = nx.multipartite_layout(G, subset_key=\"layer\")\n\nfig, ax = plt.subplots()\nnx.draw_networkx(G, pos=pos, ax=ax)\nax.set_title(\"DAG layout in topological order\")\nfig.tight_layout()\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
}

Generated by dwww version 1.16 on Wed Apr 8 10:56:14 CEST 2026.