In [1]:
%matplotlib inline
from __future__ import division
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
P = np.array([[0.2, 0.5, 0.2],
[0, 0.5, 0.5],
[0, 0, 1]])
G = nx.MultiDiGraph()
labels={}
edge_labels={}
states = ['good', 'bad', 'mediocre']
for i, origin_state in enumerate(states):
for j, destination_state in enumerate(states):
rate = P[i][j]
if rate > 0:
G.add_edge(origin_state,
destination_state,
label="{:.02f}".format(rate))
edge_labels[(origin_state, destination_state)] = label="{:.02f}".format(rate)
In [5]:
plt.figure(figsize=(14,7))
node_size = 600
#pos = nx.random_layout(G)
pos = {'bad':[0,0], 'mediocre': [0,2], 'good': [1,1]}
nx.draw_networkx_edges(G,pos, node_size=node_size)
nx.draw_networkx_labels(G, pos, font_weight=2, node_size=node_size)
nx.draw_networkx_edge_labels(G, pos, edge_labels, node_size=node_size)
plt.axis('off');
In [6]:
nx.write_dot(G,'G.dot')
In [7]:
!neato -Tps -Goverlap=scale G.dot -o G.ps; convert G.ps G.png
In [ ]: