{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Community Node Layout / Bundled Edges\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport networkx as nx\n\nfrom netgraph import Graph\n\n# create a modular graph\npartition_sizes = [10, 20, 30, 40]\ng = nx.random_partition_graph(partition_sizes, 0.5, 0.1)\n\n# create a dictionary that maps nodes to the community they belong to\nnode_to_community = dict()\nnode = 0\nfor community_id, size in enumerate(partition_sizes):\n    for _ in range(size):\n        node_to_community[node] = community_id\n        node += 1\n\ncommunity_to_color = {\n    0 : 'tab:blue',\n    1 : 'tab:orange',\n    2 : 'tab:green',\n    3 : 'tab:red',\n}\nnode_color = {node: community_to_color[community_id] for node, community_id in node_to_community.items()}\n\nGraph(g,\n      node_color=node_color, node_edge_width=0, edge_alpha=0.1,\n      node_layout='community', node_layout_kwargs=dict(node_to_community=node_to_community),\n      edge_layout='bundled', edge_layout_kwargs=dict(k=2000),\n)\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Alternatively, the best partition into communities can be inferred, for example\nusing the Louvain algorithm (:code:`pip install python-louvain`):\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from community import community_louvain\nnode_to_community = community_louvain.best_partition(g)"
      ]
    }
  ],
  "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
}