Skip to content

Commit d2e7f22

Browse files
authored
Merge branch 'up' into self-loops-curve
2 parents f1f2760 + bbdc0d3 commit d2e7f22

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ gplot(h)
160160

161161
# Keyword Arguments
162162
+ `layout` Layout algorithm: `random_layout`, `circular_layout`, `spring_layout`, `shell_layout`, `stressmajorize_layout`, `spectral_layout`. Default: `spring_layout`
163+
+ `title` Plot title. Default: `""`
164+
+ `title_color` Plot title color. Default: `colorant"black"`
165+
+ `title_size` Plot title size. Default: `4.0`
166+
+ `font_family` Font family for all text. Default: `"Helvetica"`
163167
+ `NODESIZE` Max size for the nodes. Default: `3.0/sqrt(N)`
164168
+ `nodesize` Relative size for the nodes, can be a Vector. Default: `1.0`
165169
+ `nodelabel` Labels for the vertices, a Vector or nothing. Default: `nothing`

src/layout.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ julia> locs_x, locs_y = spring_layout(g)
102102
```
103103
"""
104104
function spring_layout(g::AbstractGraph,
105-
locs_x=2*rand(nv(g)).-1.0,
106-
locs_y=2*rand(nv(g)).-1.0;
105+
locs_x::Vector{R1}=2*rand(nv(g)).-1.0,
106+
locs_y::Vector{R2}=2*rand(nv(g)).-1.0;
107107
C=2.0,
108108
MAXITER=100,
109-
INITTEMP=2.0)
109+
INITTEMP=2.0) where {R1 <: Real, R2 <: Real}
110110

111111
nvg = nv(g)
112112
adj_matrix = adjacency_matrix(g)
@@ -174,7 +174,7 @@ end
174174

175175
using Random: MersenneTwister
176176

177-
function spring_layout(g::AbstractGraph, seed::Integer, kws...)
177+
function spring_layout(g::AbstractGraph, seed::Integer; kws...)
178178
rng = MersenneTwister(seed)
179179
spring_layout(g, 2 .* rand(rng, nv(g)) .- 1.0, 2 .* rand(rng,nv(g)) .- 1.0; kws...)
180180
end
@@ -205,20 +205,20 @@ function shell_layout(g, nlist::Union{Nothing, Vector{Vector{Int}}} = nothing)
205205
if nv(g) == 1
206206
return [0.0], [0.0]
207207
end
208-
if nlist == nothing
208+
if isnothing(nlist)
209209
nlist = [collect(1:nv(g))]
210210
end
211211
radius = 0.0
212212
if length(nlist[1]) > 1
213213
radius = 1.0
214214
end
215-
locs_x = Float64[]
216-
locs_y = Float64[]
215+
locs_x = zeros(nv(g))
216+
locs_y = zeros(nv(g))
217217
for nodes in nlist
218218
# Discard the extra angle since it matches 0 radians.
219219
θ = range(0, stop=2pi, length=length(nodes)+1)[1:end-1]
220-
append!(locs_x, radius*cos.(θ))
221-
append!(locs_y, radius*sin.(θ))
220+
locs_x[nodes] = radius*cos.(θ)
221+
locs_y[nodes] = radius*sin.(θ)
222222
radius += 1.0
223223
end
224224
return locs_x, locs_y

src/plot.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ Layout algorithm. Currently can be one of [`random_layout`,
2323
`spectral_layout`].
2424
Default: `spring_layout`
2525
26+
`title`
27+
Plot title. Default: `""`
28+
29+
`title_color`
30+
Plot title color. Default: `colorant"black"`
31+
32+
`title_size`
33+
Plot title size. Default: `4.0`
34+
35+
`font_family`
36+
Font family for all text. Default: `"Helvetica"`
37+
2638
`NODESIZE`
2739
Max size for the nodes. Default: `3.0/sqrt(N)`
2840
@@ -97,6 +109,10 @@ Default: `π/5 (36 degrees)`
97109
"""
98110
function gplot(g::AbstractGraph{T},
99111
locs_x_in::Vector{R1}, locs_y_in::Vector{R2};
112+
title = "",
113+
title_color = colorant"black",
114+
title_size = 4.0,
115+
font_family = "Helvetica",
100116
nodelabel = nothing,
101117
nodelabelc = colorant"black",
102118
nodelabelsize = 1.0,
@@ -216,8 +232,10 @@ function gplot(g::AbstractGraph{T},
216232
lines, larrows = build_straight_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
217233
end
218234

219-
compose(context(units=UnitBox(-1.2, -1.2, +2.4, +2.4)),
220-
compose(context(), texts, fill(nodelabelc), stroke(nothing), fontsize(nodelabelsize)),
235+
title_offset = isempty(title) ? 0 : 0.1*title_size/4
236+
compose(context(units=UnitBox(-1.2, -1.2 - title_offset, +2.4, +2.4 + title_offset)),
237+
compose(context(), text(0, -1.2 - title_offset/2, title, hcenter, vcenter), fill(title_color), fontsize(title_size), font(font_family)),
238+
compose(context(), texts, fill(nodelabelc), fontsize(nodelabelsize), font(font_family)),
221239
compose(context(), nodes, fill(nodefillc), stroke(nodestrokec), linewidth(nodestrokelw)),
222240
compose(context(), edgetexts, fill(edgelabelc), stroke(nothing), fontsize(edgelabelsize)),
223241
compose(context(), larrows, stroke(edgestrokec), linewidth(edgelinewidth)),

0 commit comments

Comments
 (0)