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]:
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]:
In [4]:
# use smallgraph function to load some famous graphs
g = smallgraph(:karate)
gplot(g)
Out[4]:
In [5]:
nodelabel = collect(1:nv(g))
gplot(g, nodelabel=nodelabel)
Out[5]:
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]:
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]:
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]:
In [9]:
gplot(g, layout=random_layout, nodelabel=nodelabel)
Out[9]:
In [10]:
# circular layout
gplot(g, layout=circular_layout, nodelabel=nodelabel)
Out[10]:
In [11]:
# spectral layout
gplot(g, layout=spectral_layout)
Out[11]:
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]:
In [13]:
gplot(g, nodelabel=nodelabel, linetype="curve", outangle=π/11)
Out[13]:
In [14]:
h = wheel_graph(10)
gplot(h)
Out[14]:
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))
Out[15]:
GraphViz
library.Graph.jl
graphs using lualatex/tikz
.spring
and stressmajorize
layout algorithms (GraphLayout.jl)