# How to draw a directed graph of transition matrix

**URL:** https://discourse.julialang.org/t/how-to-draw-a-directed-graph-of-transition-matrix/87399
**Category:** Graphs
**Tags:** question, plotting, graphs
**Created:** [September 17, 2022, 10:41am UTC](https://discourse.julialang.org/t/how-to-draw-a-directed-graph-of-transition-matrix/87399 "2022-09-17T10:41:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [September 17, 2022, 10:41am UTC](https://discourse.julialang.org/t/how-to-draw-a-directed-graph-of-transition-matrix/87399/1 "2022-09-17T10:41:46Z")

</div>

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/](https://naysan.ca/2020/07/08/drawing-state-transition-diagrams-in-python/)

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [September 17, 2022, 9:42pm UTC](https://discourse.julialang.org/t/how-to-draw-a-directed-graph-of-transition-matrix/87399/2 "2022-09-17T21:42:32Z")

</div>

Creating a weighted digraph from the stochastic matrix is easy:

```julia
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/](https://juliagraphs.org/Graphs.jl/dev/first_steps/plotting/)

> [@Drawing a large network using Julia](https://discourse.julialang.org/t/drawing-a-large-network-using-julia/85793/2):
>
> [Graphs.jl](https://github.com/JuliaGraphs/Graphs.jl) is a good package for constructing graphs. You can build them in various ways, by adding vertices and edges, from matrices, etc. For drawing graphs, there are a number of packages that display graphs made with Graphs.jl: [TikzGraphs.jl](https://github.com/sisl/TikzGraphs.jl): backend: Tikz/Latex [GraphPlot.jl](https://juliagraphs.org/GraphPlot.jl/): backend: Compose.jl [SGtSNEpi.jl](https://github.com/fcdimitr/SGtSNEpi.jl): backend: Makie.jl [GraphRecipes.jl](https://github.com/JuliaPlots/GraphRecipes.jl): backend: Plots.jl [GraphMakie.jl](https://github.com/JuliaPlots/GraphMakie.jl): backend: Makie.jl [Karnak.jl](https://github.com/cormullion/Karnak.jl): backend: Luxor.jl

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [September 18, 2022, 9:59am UTC](https://discourse.julialang.org/t/how-to-draw-a-directed-graph-of-transition-matrix/87399/3 "2022-09-18T09:59:41Z")

</div>

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

I am having trouble adapting this [example](https://docs.juliaplots.org/latest/graphrecipes/examples/#Edge-Labels) to add weights to the edges. Can you please tell me what I am doing in correctly?

```julia
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.
