GraphPlot

A graph visualization package in Julia

Han Jihui

jh@mails.ccnu.edu.cn

@afternone

github.com/afternone

Get Started

Undirected Graph


In [1]:
# load needed package firstly
using LightGraphs
using GraphPlot

In [2]:
# construct a simple undirected graph
g = SimpleGraph(3)
add_edge!(g, 1, 2)
add_edge!(g, 2, 3)
add_edge!(g, 3, 1)
# just plot it
gplot(g) 
# If you are in Julia REPL 
# instead with `gplothtml(g)` to display result in a temp html file


Out[2]:

Directed Graph


In [3]:
# construct a simple undirected graph
g = SimpleGraph(3)
add_edge!(g, 1, 2)
add_edge!(g, 2, 3)
add_edge!(g, 3, 1)
# just plot it
gplot(g)


Out[3]:

More Complicated Example

Karate Network


In [4]:
# use smallgraph function to load some famous graphs
g = smallgraph(:karate)
gplot(g)


Out[4]:

Add node labels


In [5]:
nodelabel = collect(1:nv(g))
gplot(g, nodelabel=nodelabel)


Out[5]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

Control the node and nodelabel size


In [6]:
mysize = Float64[outdegree(g, v) for v in vertices(g)]
mysize .^= 0.3 # zoom factor
gplot(g, nodelabel=nodelabel, nodesize=mysize, nodelabelsize=mysize)


Out[6]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

Transparent


In [7]:
using Colors
# stick out large degree nodes
alphas = mysize/maximum(mysize)
nodefillc = [RGBA(0.0,0.8,0.8,i) for i in alphas]
gplot(g, nodefillc=nodefillc)


Out[7]:

Color the graph


In [8]:
# nodes membership
membership = [1,1,1,1,1,1,1,1,2,1,1,1,1,1,2,2,1,1,2,1,2,1,2,2,2,2,2,2,2,2,2,2,2,2]
nodecolor = [colorant"lightseagreen", colorant"orange"]
# membership color
nodefillc = nodecolor[membership]
gplot(g, nodefillc=nodefillc, nodelabel=nodelabel)


Out[8]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

Different layouts

Random Layout


In [9]:
gplot(g, layout=random_layout, nodelabel=nodelabel)


Out[9]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

Circular Layout


In [10]:
# circular layout
gplot(g, layout=circular_layout, nodelabel=nodelabel)


Out[10]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

Spectral Layout


In [11]:
# spectral layout
gplot(g, layout=spectral_layout)


Out[11]:

Shell Layout


In [12]:
# shell layout
nlist = Vector{Vector{Int}}() # two shells
push!(nlist, collect(1:5)) # first shell
push!(nlist, collect(6:nv(g))) # second shell
locs_x, locs_y = shell_layout(g, nlist)
gplot(g, locs_x, locs_y, nodelabel=nodelabel)


Out[12]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

Curve Edge


In [13]:
gplot(g, nodelabel=nodelabel, linetype="curve", outangle=π/11)


Out[13]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

Native LightGraphs integration


In [14]:
h = wheel_graph(10)
gplot(h)


Out[14]:

Save figure to file


In [15]:
using Cairo, Compose
# save to pdf
draw(PDF("karate.pdf", 16cm, 16cm), gplot(g))
# save to png
draw(PNG("karate.png", 16cm, 16cm), gplot(g))
# save to svg
draw(SVG("karate.svg", 16cm, 16cm), gplot(g))


WARNING: using Compose.h in module Main conflicts with an existing identifier.
WARNING: Cairo._jl_libpango is deprecated, use Cairo.libpango instead.
  likely near /home/simon/.julia/packages/IJulia/yLI42/src/kernel.jl:52
WARNING: Cairo._jl_libpango is deprecated, use Cairo.libpango instead.
  likely near /home/simon/.julia/packages/IJulia/yLI42/src/kernel.jl:52
WARNING: Cairo._jl_libpango is deprecated, use Cairo.libpango instead.
  likely near /home/simon/.julia/packages/IJulia/yLI42/src/kernel.jl:52
in apply_property at /home/simon/.julia/packages/Compose/aVZwV/src/cairo_backends.jl
WARNING: Cairo._jl_libpango is deprecated, use Cairo.libpango instead.
  likely near /home/simon/.julia/packages/IJulia/yLI42/src/kernel.jl:52
in apply_property at /home/simon/.julia/packages/Compose/aVZwV/src/cairo_backends.jl
WARNING: Cairo._jl_libpango is deprecated, use Cairo.libpango instead.
  likely near /home/simon/.julia/packages/IJulia/yLI42/src/kernel.jl:52
WARNING: Cairo._jl_libpango is deprecated, use Cairo.libpango instead.
  likely near /home/simon/.julia/packages/IJulia/yLI42/src/kernel.jl:52
WARNING: Cairo._jl_libpango is deprecated, use Cairo.libpango instead.
  likely near /home/simon/.julia/packages/IJulia/yLI42/src/kernel.jl:52
in apply_property at /home/simon/.julia/packages/Compose/aVZwV/src/cairo_backends.jl
WARNING: Cairo._jl_libpango is deprecated, use Cairo.libpango instead.
  likely near /home/simon/.julia/packages/IJulia/yLI42/src/kernel.jl:52
in apply_property at /home/simon/.julia/packages/Compose/aVZwV/src/cairo_backends.jl
Out[15]:
false

Other graph visualization packages

  • GraphLayout.jl - Graph layout and visualization algorithms, implemented in Julia.
  • GraphViz.jl - Julia binding to the GraphViz library.
  • TikzGraphs.jl - plot Graph.jl graphs using lualatex/tikz.

Thanks