pythonmathstatistics

Render a DAG into a Jupyter notebook interactively?


If you know how, great and thanks - please do tell! Auxiliary question, how do you define a default order such that the nodes are arranged vertically / any which way?

My current code:

`

import networkx as nx
import matplotlib.pyplot as plt

# Define the graph (nb only need to define the edges; the nodes are to be inferred)
edges=[("$S_{t-2}$", "$S_{t-1}$"),
       ("$S_{t-1}$", "$S_t$")]

# Construct the graph using networkx
G = nx.DiGraph()
for edge in edges:
    G.add_edge(edge[0], edge[1])

# Render the graph with mpl
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True, node_size=3000, node_color="skyblue", font_size=20, font_weight="bold", font_color="black", edge_color="black", linewidths=2, arrowsize=30)
plt.show()

The above works fine:

[My graph]

But I'd like to produce an interactive graph (where you can move the nodes about) in a fun 'springy' fashion but with sensible/regular defaults?

I've been trying for a about 0.5 day - so any help much appreciated!


Solution

  • So one solution would be to use the cytoscape library that allows you to draw interactive graphs in the browser.

    Since this is Javascript, you need bindings for Python that will work well with jupyter. I see two