{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Long chain of connections using Sankey\n\nDemonstrate/test the Sankey class by producing a long chain of connections.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom matplotlib.sankey import Sankey\n\nlinks_per_side = 6\n\n\ndef side(sankey, n=1):\n    \"\"\"Generate a side chain.\"\"\"\n    prior = len(sankey.diagrams)\n    for i in range(0, 2*n, 2):\n        sankey.add(flows=[1, -1], orientations=[-1, -1],\n                   patchlabel=str(prior + i),\n                   prior=prior + i - 1, connect=(1, 0), alpha=0.5)\n        sankey.add(flows=[1, -1], orientations=[1, 1],\n                   patchlabel=str(prior + i + 1),\n                   prior=prior + i, connect=(1, 0), alpha=0.5)\n\n\ndef corner(sankey):\n    \"\"\"Generate a corner link.\"\"\"\n    prior = len(sankey.diagrams)\n    sankey.add(flows=[1, -1], orientations=[0, 1],\n               patchlabel=str(prior), facecolor='k',\n               prior=prior - 1, connect=(1, 0), alpha=0.5)\n\n\nfig = plt.figure()\nax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],\n                     title=\"Why would you want to do this?\\n(But you could.)\")\nsankey = Sankey(ax=ax, unit=None)\nsankey.add(flows=[1, -1], orientations=[0, 1],\n           patchlabel=\"0\", facecolor='k',\n           rotation=45)\nside(sankey, n=links_per_side)\ncorner(sankey)\nside(sankey, n=links_per_side)\ncorner(sankey)\nside(sankey, n=links_per_side)\ncorner(sankey)\nside(sankey, n=links_per_side)\nsankey.finish()\n# Notice:\n# 1. The alignment doesn't drift significantly (if at all; with 16007\n#    subdiagrams there is still closure).\n# 2. The first diagram is rotated 45 deg, so all other diagrams are rotated\n#    accordingly.\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. admonition:: References\n\n   The use of the following functions, methods, classes and modules is shown\n   in this example:\n\n   - `matplotlib.sankey`\n   - `matplotlib.sankey.Sankey`\n   - `matplotlib.sankey.Sankey.add`\n   - `matplotlib.sankey.Sankey.finish`\n\n"
      ]
    }
  ],
  "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.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}