.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "sphinx_gallery_animations/plot_02_animate_edges.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_animations_plot_02_animate_edges.py: Visualise Changes in Edge Weights ================================= Here, we demonstrate how to visualise changes in edge weights over time. We change both, the colour and the width of the edges depending on the weight. .. GENERATED FROM PYTHON SOURCE LINES 9-49 .. container:: sphx-glr-animation .. raw:: html
.. code-block:: default import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from netgraph import Graph # Simulate a dynamic network with # - 5 frames / network states, # - with 10 nodes at each time point, # - an expected edge density of 25%, and # - edge weights drawn from a Gaussian distribution. total_frames = 5 total_nodes = 10 adjacency_matrix = np.random.rand(total_nodes, total_nodes) < 0.25 weight_matrix = np.random.randn(total_frames, total_nodes, total_nodes) # Normalise the weights, such that they are on the interval [0, 1]. # They can then be passed directly to matplotlib colormaps (which expect floats on that interval). vmin, vmax = -2, 2 weight_matrix[weight_matrixvmax] = vmax weight_matrix -= vmin weight_matrix /= vmax - vmin cmap = plt.cm.RdGy fig, ax = plt.subplots() g = Graph(adjacency_matrix, edge_cmap=cmap, arrows=True, ax=ax) def update(ii): artists = [] for jj, kk in zip(*np.where(adjacency_matrix)): w = weight_matrix[ii, jj, kk] artist = g.edge_artists[(jj, kk)] artist.set_facecolor(cmap(w)) artist.update_width(0.03 * np.abs(w-0.5)) # np.abs(w-0.5) so that large negative edges are also wide artists.append(artist) return artists animation = FuncAnimation(fig, update, frames=total_frames, interval=200, blit=True) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 1.265 seconds) .. _sphx_glr_download_sphinx_gallery_animations_plot_02_animate_edges.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_02_animate_edges.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_animate_edges.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_