.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/ring_animation.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_ring_animation.py: .. _tutorials-ring-animation: ==================== Ring Graph Animation ==================== This example demonstrates how to use :doc:`matplotlib:api/animation_api` in order to animate a ring graph sequentially being revealed. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: Python import igraph as ig import matplotlib.pyplot as plt import matplotlib.animation as animation .. GENERATED FROM PYTHON SOURCE LINES 20-21 Create a ring graph, which we will then animate .. GENERATED FROM PYTHON SOURCE LINES 21-23 .. code-block:: Python g = ig.Graph.Ring(10, directed=True) .. GENERATED FROM PYTHON SOURCE LINES 24-25 Compute a 2D ring layout that looks like an actual ring .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: Python layout = g.layout_circle() .. GENERATED FROM PYTHON SOURCE LINES 29-39 Prepare an update function. This "callback" function will be run at every frame and takes as a single argument the frame number. For simplicity, at each frame we compute a subgraph with only a fraction of the vertices and edges. As time passes, the graph becomes more and more complete until the whole ring is closed. .. note:: The beginning and end of the animation are a little tricky because only a vertex or edge is added, not both. Don't worry if you cannot understand all details immediately. .. GENERATED FROM PYTHON SOURCE LINES 39-77 .. code-block:: Python def _update_graph(frame): # Remove plot elements from the previous frame ax.clear() # Fix limits (unless you want a zoom-out effect) ax.set_xlim(-1.5, 1.5) ax.set_ylim(-1.5, 1.5) if frame < 10: # Plot subgraph gd = g.subgraph(range(frame)) elif frame == 10: # In the second-to-last frame, plot all vertices but skip the last # edge, which will only be shown in the last frame gd = g.copy() gd.delete_edges(9) else: # Last frame gd = g ig.plot(gd, target=ax, layout=layout[:frame], vertex_color="yellow") # Capture handles for blitting if frame == 0: nhandles = 0 elif frame == 1: nhandles = 1 elif frame < 11: # vertex, 2 for each edge nhandles = 3 * frame else: # The final edge closing the circle nhandles = 3 * (frame - 1) + 2 handles = ax.get_children()[:nhandles] return handles .. GENERATED FROM PYTHON SOURCE LINES 78-79 Run the animation .. GENERATED FROM PYTHON SOURCE LINES 79-84 .. code-block:: Python fig, ax = plt.subplots() ani = animation.FuncAnimation(fig, _update_graph, 12, interval=500, blit=True) plt.ion() plt.show() .. container:: sphx-glr-animation .. raw:: html
.. GENERATED FROM PYTHON SOURCE LINES 85-93 .. note:: We use *igraph*'s :meth:`Graph.subgraph()` (see :meth:`igraph.GraphBase.induced_subgraph`) in order to obtain a section of the ring graph at a time for each frame. While sufficient for an easy example, this approach is not very efficient. Thinking of more efficient approaches, e.g. vertices with zero radius, is a useful exercise to learn the combination of igraph and matplotlib. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.160 seconds) .. _sphx_glr_download_tutorials_ring_animation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ring_animation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ring_animation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: ring_animation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_