
.. 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

        Click :ref:`here <sphx_glr_download_tutorials_visualize_communities.py>`
        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-13

.. code-block:: default

    import igraph as ig
    import matplotlib.pyplot as plt








.. GENERATED FROM PYTHON SOURCE LINES 14-15

First, we generate a graph. We use a famous graph here for simplicity:

.. GENERATED FROM PYTHON SOURCE LINES 15-17

.. code-block:: default

    g = ig.Graph.Famous("Zachary")








.. GENERATED FROM PYTHON SOURCE LINES 18-20

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 20-23

.. code-block:: default

    communities = g.community_edge_betweenness()
    communities = communities.as_clustering()








.. GENERATED FROM PYTHON SOURCE LINES 24-25

Next, we color each vertex and edge based on its community membership:

.. GENERATED FROM PYTHON SOURCE LINES 25-33

.. code-block:: default

    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 34-37

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 37-65

.. code-block:: default

    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 66-68

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.550 seconds)


.. _sphx_glr_download_tutorials_visualize_communities.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: visualize_communities.py <visualize_communities.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: visualize_communities.ipynb <visualize_communities.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
