dwww Home | Show directory contents | Find package

{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Visibility Graph\n\nVisibility Graph constructed from a time series\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from matplotlib import pyplot as plt\n\nimport networkx as nx\n\ntime_series = [0, 2, 1, 3, 2, 1, 3, 2, 1, 3, 2, 1, 3, 4, 0]\n# or\n# import random\n# time_series = [random.randint(1, 10) for i in range(10)]\n\nG = nx.visibility_graph(time_series)\n\nlabels = nx.get_node_attributes(G, \"value\")\n\nfig, all_axes = plt.subplots(2, 1, num=\"Visibility Graph\", figsize=(8, 12))\naxs = all_axes.flat\n\nlayouts_params = {\n    # a layout emphasizing the line-of-sight connectivity\n    \"Line-of-Sight Connectivity\": {\n        \"pos\": {x: (x, 0) for x in range(len(time_series))},\n        \"connectionstyle\": \"arc3,rad=-1.57079632679\",\n    },\n    # a layout showcasing the time series values\n    \"Time Series values with Connectivity\": {\n        \"pos\": {i: (i, v) for i, v in enumerate(time_series)}\n    },\n}\n\nfor i, (name, params) in enumerate(layouts_params.items()):\n    axs[i].title.set_text(name)\n    axs[i].title.set_size(11)\n    axs[i].set_xlabel(\"Time\", size=10)\n    axs[i].margins(0.10)\n    nx.draw_networkx_nodes(G, params.get(\"pos\"), ax=axs[i], alpha=0.5)\n    nx.draw_networkx_labels(G, params.get(\"pos\"), ax=axs[i], labels=labels)\n    nx.draw_networkx_edges(\n        G, **params, ax=axs[i], arrows=True, arrowstyle=\"<->\", arrowsize=10\n    )\n\naxs[1].set_ylabel(\"Value\", size=10)\n\nfig.suptitle(\"Visibility Graph\")\nfig.tight_layout()\nplt.show()"
      ]
    }
  ],
  "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.12.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}

Generated by dwww version 1.16 on Wed Apr 8 11:18:11 CEST 2026.