How to draw a directed graph of transition matrix

Hi all,

I would like to draw a network to visualize a transition matrix. I was wondering if there is a package somewhere that automatically generates a directed graph from a transition matrix. Here is an example of what I am aiming for:

https://naysan.ca/2020/07/08/drawing-state-transition-diagrams-in-python/

1 Like

Creating a weighted digraph from the stochastic matrix is easy:

julia> using Graphs, SimpleWeightedGraphs

julia> m = [0.8 0.2; 0.1 0.9]
2×2 Matrix{Float64}:
 0.8  0.2
 0.1  0.9

julia> map(weight, edges(SimpleWeightedDiGraph(m)))
4-element Vector{Float64}:
 0.8
 0.2
 0.1
 0.9

For how to draw the graph, see here:

https://juliagraphs.org/Graphs.jl/dev/first_steps/plotting/

1 Like

Thank you for your reply. SimpleWeightedDigraph was exactly what I needed.

I am having trouble adapting this example to add weights to the edges. Can you please tell me what I am doing in correctly?

using SimpleWeightedGraphs, Graphs

using Plots, GraphRecipes

m = [0.8 0.2; 0.1 0.9]

g = SimpleWeightedDiGraph(m)

edge_label = Dict((i,j) => string(m[i,j]) for i in 1:size(m, 1), j in 1:size(m, 2))

graphplot(g; names = 1:length(m), edge_label)

Update

I mistyped the keyword as edge_labels instead of edge_label. I corrected the error above and will mark this as a solution. Thank you for your help.

2 Likes