.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "sphinx_gallery_output/plot_10_community_layout.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_sphinx_gallery_output_plot_10_community_layout.py: Community Node Layout / Bundled Edges ===================================== .. GENERATED FROM PYTHON SOURCE LINES 6-40 .. code-block:: default import matplotlib.pyplot as plt import networkx as nx from netgraph import Graph # create a modular graph partition_sizes = [10, 20, 30, 40] g = nx.random_partition_graph(partition_sizes, 0.5, 0.1) # create a dictionary that maps nodes to the community they belong to node_to_community = dict() node = 0 for community_id, size in enumerate(partition_sizes): for _ in range(size): node_to_community[node] = community_id node += 1 community_to_color = { 0 : 'tab:blue', 1 : 'tab:orange', 2 : 'tab:green', 3 : 'tab:red', } node_color = {node: community_to_color[community_id] for node, community_id in node_to_community.items()} Graph(g, node_color=node_color, node_edge_width=0, edge_alpha=0.1, node_layout='community', node_layout_kwargs=dict(node_to_community=node_to_community), edge_layout='bundled', edge_layout_kwargs=dict(k=2000), ) plt.show() .. image-sg:: /sphinx_gallery_output/images/sphx_glr_plot_10_community_layout_001.png :alt: plot 10 community layout :srcset: /sphinx_gallery_output/images/sphx_glr_plot_10_community_layout_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none /home/paul/src/netgraph/netgraph/_edge_layout.py:692: RuntimeWarning: invalid value encountered in true_divide displacement = compatibility * delta / distance_squared[..., None] .. GENERATED FROM PYTHON SOURCE LINES 41-43 Alternatively, the best partition into communities can be inferred, for example using the Louvain algorithm (:code:`pip install python-louvain`): .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: default from community import community_louvain node_to_community = community_louvain.best_partition(g) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 3 minutes 59.103 seconds) .. _sphx_glr_download_sphinx_gallery_output_plot_10_community_layout.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_10_community_layout.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_10_community_layout.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_