Drawing State Transition Diagrams in Python – Improved Version

You’ve got to love the open-source community: thanks to updates made by @yagoduppel, the Markov Chain GitHub repository can now support more than four states. Also it looks way nicer than before.

So here are updated examples of how to easily draw state transition diagrams in Python.

Basic Install

```# Basically you just import it as a module
from markovchain import MarkovChain```

Two States

```P = np.array([[0.8, 0.2], [0.1, 0.9]]) # Transition matrix
mc = MarkovChain(P, ['1', '2'])
mc.draw("../img/markov-chain-two-states.png")```

Three States

```P = np.array([
[0.8, 0.1, 0.1],
[0.1, 0.7, 0.2],
[0.1, 0.7, 0.2],
])
mc = MarkovChain(P, ['A', 'B', 'C'])
mc.draw("../img/markov-chain-three-states.png")```

Four States

```P = np.array([
[0.8, 0.1, 0.1, 0.0],
[0.1, 0.7, 0.0, 0.2],
[0.1, 0.0, 0.7, 0.2],
[0.1, 0.0, 0.7, 0.2]
])
mc = MarkovChain(P, ['1', '2', '3', '4'])
mc.draw("../img/markov-chain-four-states.png")```

Five States

```P = np.array([
[0.8, 0.1, 0.1, 0.0, 0.0],
[0.1, 0.6, 0.0, 0.2, 0.1],
[0.1, 0.0, 0.7, 0.2, 0.0],
[0.1, 0.0, 0.4, 0.2, 0.3],
[0.6, 0.1, 0.1, 0.0, 0.2],
])
mc = MarkovChain(P, ['1', '2', '3', '4', '5'])
mc.draw("../img/markov-chain-five-states.png")```

And you can keep going.