main

2020/05/08

Visualizing Markov Chains with NetworkX

Here is the original post I found out https://vknight.org/unpeudemath/code/2015/11/15/Visualising-markov-chains.html.

However, there are some places which are not clear to me at first. After some debug, I decided to put out a simpler version for people to use.

Assuming a Markov chain transition matrix T ( 5x5 matrix) with majority of them are zero.

T(0,0) = 0.1
T(0,1) = 0.2
T(0,3) = 0.7

T(1,1) = 0.9

https://stackoverflow.com/questions/13814640/color-a-particular-node-in-networkx-and-graphviz

import networkx as nx
from networkx.drawing.nx_agraph import to_agraph

G = nx.Graph()

G.add_node(1,color='red',style='filled',fillcolor='blue',shape='square')
G.add_node(2,color='blue',style='filled')
G.add_edge(1,2,color='green')
G.nodes[2]['shape']='circle'
G.nodes[2]['fillcolor']='red'

A = to_agraph(G)
A.layout()
A.draw('color.png')
print(A.to_string())

No comments:

Post a Comment

How to Supercharge Your Python Classes with Class Methods

  How to Supercharge Your Python Classes with Class Methods | by Siavash Yasini | May, 2024 | Towards Data Science As we just mentioned, a c...