.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/other/plot_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_gallery_other_plot_animation.py: Animations ========== This tutorial shows how to animate `iplotx` visualizations using `matplotlib.animation.FuncAnimation`. For illustration purposes, we will animate a simple directed graph, rotating it around its center. We also modify the opacity of the vertices and edges, just for fun. .. GENERATED FROM PYTHON SOURCE LINES 12-65 .. container:: sphx-glr-animation .. raw:: html
.. code-block:: Python import matplotlib.pyplot as plt import numpy as np import matplotlib.animation as animation import igraph as ig import iplotx as ipx g = ig.Graph.Ring(3, directed=True) layout = np.asarray(g.layout("circle").coords) # The animation will rotate the layout of the graph thetas = np.linspace(0, 2 * np.pi, 101)[:-1] fig, ax = plt.subplots() art = ipx.network(g, ax=ax, layout=layout, aspect=1)[0] ax.set(xlim=[-2, 2], ylim=[-2, 2]) def rotate(vector, theta): """Rotate a 2D vector by an angle theta (in radians) clockwise.""" return vector @ np.array( [ [np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)], ], ) def update(frame): # for each frame, update the vertex positions theta = thetas[frame] # The new layout is the old layout rotated by theta, clockwise layout_new = rotate(layout, theta) # Also change transparency, slower alpha_vertices = 1 - np.abs(np.sin(theta / 2)) alpha_edges = 0.25 + 0.75 * np.abs(np.sin(theta / 2)) # Edit the vertices' positions art.get_vertices().set_offsets(layout_new) art.get_vertices().set_alpha(alpha_vertices) art.get_edges().set_alpha(alpha_edges) return (art.get_vertices(), art.get_edges()) # Run the animation ani = animation.FuncAnimation( fig=fig, func=update, frames=len(thetas), interval=30, ) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 8.948 seconds) .. _sphx_glr_download_gallery_other_plot_animation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_animation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_animation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_animation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_