Graph layout and visualization algorithms based on Compose.jl and inspired by GraphLayout.jl.
The spring_layout and stressmajorize_layout function are copy from IainNZ's GraphLayout.jl.
Other layout algorithms are wrapped from NetworkX.
gadfly.js is copied from Gadfly.jl
From the Julia REPL the latest version can be installed with
Pkg.add("GraphPlot")GraphPlot is then loaded with
using GraphPlotusing Graphs: smallgraph
g = smallgraph(:karate)
gplot(g)
using Graphs
nodelabel = 1:nv(g)
gplot(g, nodelabel=nodelabel)
gplot(g, nodelabel=nodelabel, nodelabeldist=1.5, nodelabelangleoffset=π/4)# nodes size proportional to their degree
nodesize = [Graphs.outdegree(g, v) for v in Graphs.vertices(g)]
gplot(g, nodesize=nodesize)Feed the keyword argument nodefillc a color array, ensure each node has a color. length(nodefillc) must be equal |V|.
using Colors
# Generate n maximally distinguishable colors in LCHab space.
nodefillc = distinguishable_colors(nv(g), colorant"blue")
gplot(g, nodefillc=nodefillc, nodelabel=nodelabel, nodelabeldist=1.8, nodelabelangleoffset=π/4)# stick out large degree nodes
alphas = nodesize/maximum(nodesize)
nodefillc = [RGBA(0.0,0.8,0.8,i) for i in alphas]
gplot(g, nodefillc=nodefillc)nodelabelsize = nodesize
gplot(g, nodelabelsize=nodelabelsize, nodesize=nodesize, nodelabel=nodelabel)edgelabel = 1:Graphs.ne(g)
gplot(g, edgelabel=edgelabel, nodelabel=nodelabel)edgelabel = 1:Graphs.ne(g)
gplot(g, edgelabel=edgelabel, nodelabel=nodelabel, edgelabeldistx=0.5, edgelabeldisty=0.5)# 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)This is the defaut layout and will be chosen if no layout is specified. The default parameters to the spring layout algorithm can be changed by supplying an anonymous function, e.g., if nodes appear clustered too tightly together, try
layout=(args...)->spring_layout(args...; C=20)
gplot(g, layout=layout, nodelabel=nodelabel)where C influences the desired distance between nodes.
gplot(g, layout=random_layout, nodelabel=nodelabel)gplot(g, layout=circular_layout, nodelabel=nodelabel)gplot(g, layout=spectral_layout)nlist = Vector{Vector{Int}}(undef, 2) # two shells
nlist[1] = 1:5 # first shell
nlist[2] = 6:nv(g) # second shell
locs_x, locs_y = shell_layout(g, nlist)
gplot(g, locs_x, locs_y, nodelabel=nodelabel)gplot(g, linetype="curve")When using an IDE such as VSCode, Cairo.jl is required to visualize the plot inside the IDE.
When using the REPL, gplothtml will allow displaying the plot on a browser.
using 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))
# alternate way of saving to svg without loading Compose
saveplot(gplot(g, plot_size = (16cm, 16cm)), "karate.svg")using Graphs
h = watts_strogatz(50, 6, 0.3)
gplot(h)GGraph to drawlocs_x, locs_yLocations of the nodes (will be normalized and centered). If not specified, will be obtained fromlayoutkwarg.
layoutLayout algorithm:random_layout,circular_layout,spring_layout,shell_layout,stressmajorize_layout,spectral_layout. Default:spring_layouttitlePlot title. Default:""title_colorPlot title color. Default:colorant"black"title_sizePlot title size. Default:4.0font_familyFont family for all text. Default:"Helvetica"NODESIZEMax size for the nodes. Default:3.0/sqrt(N)nodesizeRelative size for the nodes, can be a Vector. Default:1.0nodelabelLabels for the vertices, a Vector or nothing. Default:nothingnodelabelcColor for the node labels, can be a Vector. Default:colorant"black"nodelabeldistDistances for the node labels from center of nodes. Default:0.0nodelabelangleoffsetAngle offset for the node labels. Default:π/4.0NODELABELSIZELargest fontsize for the vertice labels. Default:4.0nodelabelsizeRelative fontsize for the vertice labels, can be a Vector. Default:1.0nodefillcColor to fill the nodes with, can be a Vector. Default:colorant"turquoise"nodestrokecColor for the nodes stroke, can be a Vector. Default:nothingnodestrokelwLine width for the nodes stroke, can be a Vector. Default:0.0edgelabelLabels for the edges, a Vector or nothing. Default:[]edgelabelcColor for the edge labels, can be a Vector. Default:colorant"black"edgelabeldistx, edgelabeldistyDistance for the edge label from center of edge. Default:0.0EDGELABELSIZELargest fontsize for the edge labels. Default:4.0edgelabelsizeRelative fontsize for the edge labels, can be a Vector. Default:1.0EDGELINEWIDTHMax line width for the edges. Default:0.25/sqrt(N)edgelinewidthRelative line width for the edges, can be a Vector. Default:1.0edgestrokecColor for the edge strokes, can be a Vector. Default:colorant"lightgray"arrowlengthfracFraction of line length to use for arrows. Equal to 0 for undirected graphs. Default:0.1for the directed graphsarrowangleoffsetAngular width in radians for the arrows. Default:π/9 (20 degrees)linetypeType of line used for edges ("straight", "curve"). Default: "straight"outangleAngular width in radians for the edges (only used iflinetype = "curve). Default:π/5 (36 degrees)background_colorColor for the plot background. Default:nothingplot_sizeTuple of measures for width x height of plot area. Default:(10cm, 10cm)leftpad, rightpad, toppad, bottompadPadding for the plot margins. Default:0mmpadPadding for plot margins (overrides individual padding if given). Default:nothing
Filing an issue to report a bug, counterintuitive behavior, or even to request a feature is extremely valuable in helping me prioritize what to work on, so don't hestitate.