{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Visualise Changes in Edge Weights\n\nHere, we demonstrate how to visualise changes in edge weights over time.\nWe change both, the colour and the width of the edges depending on the weight.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\n\nfrom matplotlib.animation import FuncAnimation\nfrom netgraph import Graph\n\n# Simulate a dynamic network with\n# - 5 frames / network states,\n# - with 10 nodes at each time point,\n# - an expected edge density of 25%, and\n# - edge weights drawn from a Gaussian distribution.\ntotal_frames = 5\ntotal_nodes = 10\nadjacency_matrix = np.random.rand(total_nodes, total_nodes) < 0.25\nweight_matrix = np.random.randn(total_frames, total_nodes, total_nodes)\n\n# Normalise the weights, such that they are on the interval [0, 1].\n# They can then be passed directly to matplotlib colormaps (which expect floats on that interval).\nvmin, vmax = -2, 2\nweight_matrix[weight_matrix<vmin] = vmin\nweight_matrix[weight_matrix>vmax] = vmax\nweight_matrix -= vmin\nweight_matrix /= vmax - vmin\n\ncmap = plt.cm.RdGy\n\nfig, ax = plt.subplots()\ng = Graph(adjacency_matrix, edge_cmap=cmap, arrows=True, ax=ax)\n\ndef update(ii):\n    artists = []\n    for jj, kk in zip(*np.where(adjacency_matrix)):\n        w = weight_matrix[ii, jj, kk]\n        artist = g.edge_artists[(jj, kk)]\n        artist.set_facecolor(cmap(w))\n        artist.update_width(0.03 * np.abs(w-0.5)) # np.abs(w-0.5) so that large negative edges are also wide\n        artists.append(artist)\n    return artists\n\nanimation = FuncAnimation(fig, update, frames=total_frames, interval=200, blit=True)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.8"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}