{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Bipartite node layout\n\n\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "By default, nodes are partitioned into two subsets using a two-coloring of the graph.\nThe median heuristic proposed in Eades & Wormald (1994) is used to reduce edge crossings.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom netgraph import Graph\n\nedges = [\n    (0, 1),\n    (1, 2),\n    (2, 3),\n    (3, 4),\n    (5, 6)\n]\n\nGraph(edges, node_layout='bipartite', node_labels=True)\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The partitions can also be made explicit using the :code:`subsets` argument.\nIn multi-component bipartite graphs, multiple two-colorings are possible,\nsuch that explicit specification of the subsets may be necessary to achieve the desired partitioning of nodes.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom netgraph import Graph\n\nedges = [\n    (0, 1),\n    (1, 2),\n    (2, 3),\n    (3, 4),\n    (5, 6)\n]\n\nGraph(edges, node_layout='bipartite', node_layout_kwargs=dict(subsets=[(0, 2, 4, 6), (1, 3, 5)]), node_labels=True)\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To change the layout from the left-right orientation to a bottom-up orientation,\ncall the layout function directly and swap x and y coordinates of the node positions.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom netgraph import Graph, get_bipartite_layout\n\nedges = [\n    (0, 1),\n    (1, 2),\n    (2, 3),\n    (3, 4),\n    (5, 6)\n]\n\nnode_positions = get_bipartite_layout(edges, subsets=[(0, 2, 4, 6), (1, 3, 5)])\nnode_positions = {node : (x, y) for node, (y, x) in node_positions.items()}\n\nGraph(edges, node_layout=node_positions, node_labels=True)\n\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.6.8"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}