.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/visualize_communities.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_visualize_communities.py: .. _tutorials-visualize-communities: ===================== Communities ===================== This example shows how to visualize communities or clusters of a graph. .. GENERATED FROM PYTHON SOURCE LINES 10-14 .. code-block:: Python import igraph as ig import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 15-16 First, we generate a graph. We use a famous graph here for simplicity: .. GENERATED FROM PYTHON SOURCE LINES 16-18 .. code-block:: Python g = ig.Graph.Famous("Zachary") .. GENERATED FROM PYTHON SOURCE LINES 19-21 Edge betweenness is a standard way to detect communities. We then covert into a :class:`igraph.VertexClustering` object for subsequent ease of use: .. GENERATED FROM PYTHON SOURCE LINES 21-24 .. code-block:: Python communities = g.community_edge_betweenness() communities = communities.as_clustering() .. GENERATED FROM PYTHON SOURCE LINES 25-26 Next, we color each vertex and edge based on its community membership: .. GENERATED FROM PYTHON SOURCE LINES 26-34 .. code-block:: Python num_communities = len(communities) palette = ig.RainbowPalette(n=num_communities) for i, community in enumerate(communities): g.vs[community]["color"] = i community_edges = g.es.select(_within=community) community_edges["color"] = i .. GENERATED FROM PYTHON SOURCE LINES 35-38 Last, we plot the graph. We use a fancy technique called proxy artists to make a legend. You can find more about that in matplotlib's :doc:`matplotlib:users/explain/axes/legend_guide`: .. GENERATED FROM PYTHON SOURCE LINES 38-67 .. code-block:: Python fig, ax = plt.subplots() ig.plot( communities, palette=palette, edge_width=1, target=ax, vertex_size=20, ) # Create a custom color legend legend_handles = [] for i in range(num_communities): handle = ax.scatter( [], [], s=100, facecolor=palette.get(i), edgecolor="k", label=i, ) legend_handles.append(handle) ax.legend( handles=legend_handles, title="Community:", bbox_to_anchor=(0, 1.0), bbox_transform=ax.transAxes, ) plt.show() .. image-sg:: /tutorials/images/sphx_glr_visualize_communities_001.png :alt: visualize communities :srcset: /tutorials/images/sphx_glr_visualize_communities_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-70 For an example on how to generate the cluster graph from a vertex cluster, check out :ref:`tutorials-cluster-graph`. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.265 seconds) .. _sphx_glr_download_tutorials_visualize_communities.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: visualize_communities.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: visualize_communities.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: visualize_communities.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_