@@ -11,7 +11,6 @@ All example images on this page are created using [`Makie.jl`](https://github.co
11
11
using CairoMakie
12
12
CairoMakie.activate!(type="png") # hide
13
13
set_theme!(resolution=(800, 400)) #hide
14
- CairoMakie.inline!(true) # hide
15
14
using NetworkLayout
16
15
using GraphMakie, Graphs
17
16
nothing #hide
@@ -174,3 +173,62 @@ layout = Spectral()
174
173
f, ax, p = graphplot(g, layout=layout, node_size=0.0, edge_width=1.0)
175
174
f #hide
176
175
```
176
+
177
+ ## ` pin ` Positions in Interative Layouts
178
+ Sometimes it is desired to fix the positions of a few nodes while arranging the rest "naturally" around them.
179
+ The iterative layouts [ ` Stress ` ] ( @ref ) , [ ` Spring ` ] ( @ref ) and [ ` SFDP ` ] ( @ref ) allow to pin
180
+ nodes to certain positions, i.e. those node will stay fixed during the iteration.
181
+ ``` @example layouts
182
+ g = SimpleGraph(vcat(hcat(zeros(4,4), ones(4,4)), hcat(ones(4,4), zeros(4,4))))
183
+ nothing #hide
184
+ ```
185
+
186
+ The keyword argument ` pin ` takes a Vector or a Dict of key - value pairs. The key has to be
187
+ the index of the node. The value can take three forms:
188
+ - ` idx => Point2(x,y) ` or ` idx => (x,y) ` overwrites the initial position of that vertex and pins it there,
189
+ - ` idx => true/false ` pins or unpins the vertex, position is taken from ` initialpos ` -keyword argument or random,
190
+ - ` idx => (false, true) ` allows for fine control over which coordinate to pin.
191
+
192
+ ``` @example layouts
193
+ initialpos = Dict(1=>Point2f(-1,0.5),
194
+ 3=>Point2f(1,0),
195
+ 4=>Point2f(1,0))
196
+ pin = Dict(1=>true,
197
+ 2=>(-1,-0.5),
198
+ 3=>(true, false),
199
+ 4=>(true, false))
200
+ nothing #hide
201
+ ```
202
+ Example animation on how those keyword arguments effect different iterative layouts:
203
+ ``` @example layouts
204
+ springl = Spring(;initialpos, pin, seed=11) #2
205
+ sfdpl = SFDP(;initialpos, pin, tol=0.0)
206
+ stressl = Stress(;initialpos, pin, reltols=0.0, abstolx=0.0, iterations=100)
207
+
208
+ f = Figure(resolution=(1200,500))
209
+ ax1 = f[1,1] = Axis(f; title="Spring")
210
+ ax2 = f[1,2] = Axis(f; title="SFDP")
211
+ ax3 = f[1,3] = Axis(f; title="SFDP")
212
+
213
+ for ax in [ax1, ax2, ax3]
214
+ xlims!(ax,-2,2); ylims!(ax,-1.4,1.4); vlines!(ax, 1; color=:red); hidespines!(ax); hidedecorations!(ax)
215
+ end
216
+
217
+ node_color = vcat(:green, :green, :red, :red, [:black for _ in 1:4])
218
+ node_size = vcat([40 for _ in 1:4], [20 for _ in 1:4])
219
+ nlabels = vcat("1", "2", "3", "4", ["" for _ in 1:4])
220
+ nlabels_align = (:center, :center)
221
+ nlabels_color = :white
222
+ p1 = graphplot!(ax1, g; layout=springl, node_color, node_size, nlabels, nlabels_align, nlabels_color)
223
+ p2 = graphplot!(ax2, g; layout=sfdpl, node_color, node_size, nlabels, nlabels_align, nlabels_color)
224
+ p3 = graphplot!(ax3, g; layout=stressl, node_color, node_size, nlabels, nlabels_align, nlabels_color)
225
+
226
+ iterators = [LayoutIterator(l, g) for l in (springl, sfdpl, stressl)]
227
+ record(f, "pin_animation.mp4", zip(iterators...); framerate = 10) do (pos1, pos2, pos3)
228
+ p1[:node_pos][] = pos1
229
+ p2[:node_pos][] = pos2
230
+ p3[:node_pos][] = pos3
231
+ end
232
+ nothing #hide
233
+ ```
234
+ ![ pin animation] ( pin_animation.mp4 )
0 commit comments