Skip to content

Instantly share code, notes, and snippets.

@kingjr
Created November 27, 2018 16:04
Show Gist options
  • Save kingjr/4609b8869c2faa0de8e421d464d484ba to your computer and use it in GitHub Desktop.
Save kingjr/4609b8869c2faa0de8e421d464d484ba to your computer and use it in GitHub Desktop.
plot dyn system
"""
sudo apt-get install python-dev graphviz libgraphviz-dev pkg-config
pip install pygraphviz
"""
%matplotlib inline
import numpy as np
import os
import networkx as nx
from networkx.drawing.nx_agraph import to_agraph
from IPython.display import Image
def plot(W, O, labels):
G = nx.DiGraph(W)
# add graphviz layout options (see https://stackoverflow.com/a/39662097)
G.graph['edge'] = {'arrowsize': '1.', 'splines': 'curved',
'fontsize':10, 'fontcolor':'gray'}
G.graph['node'] = {'shape': 'circle', 'style': 'filled',
'fontcolor':'white', 'fillcolor':'k'}
G.graph['graph'] = {'scale': '3'}
# Change node color observable
for i, (obs, label) in enumerate(zip(O, labels)):
if not obs:
G.nodes[i]['fillcolor'] = 'white'
G.nodes[i]['fontcolor'] = 'k'
G.nodes[i]['label'] = label
# change arrow for negative weights
for j, edge in G[i].items():
edge['xlabel'] = W[i, j]
if W[i, j] < 0:
edge['arrowhead'] = 'dot'
A = to_agraph(G)
A.layout('dot')
A.draw('_graph.png')
return Image(filename='_graph.png')
# define the graph as per your question
W = np.zeros((3, 3))
W[0, 1] = 1
W[1, 1] = .99
W[1, 2] = .1
W[2, 2] = .99
W[2, 1] = -.1
# observables
O = np.zeros(3)
O[0] = 1
O[1] = 1
plot(W=W, O=[1, 1, 0], labels=['u', '<x<SUB>1</SUB>>', '<x<SUB>2</SUB>>'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment