JuliaGraphs / GraphPlot.jl

Graph visualization for Julia.
http://JuliaGraphs.github.io/GraphPlot.jl/
Other
200 stars 62 forks source link

`C` kwarg failing in `spring_layout` #154

Closed SimonEnsemble closed 2 months ago

SimonEnsemble commented 2 years ago

the following code fails:

# make a graph with three connected components
graph = SimpleGraph(9)
add_edge!(graph, 1, 2)

add_edge!(graph, 3, 4)
add_edge!(graph, 3, 5)
add_edge!(graph, 4, 5)

add_edge!(graph, 6, 7)
add_edge!(graph, 7, 8)
add_edge!(graph, 8, 9)

loc_x, loc_y = spring_layout(graph, 97339, C=2.0)

with the error:

MethodError: no method matching setindex!(::Int64, ::Float64, ::Int64)

    var"#spring_layout#2"(::Float64, ::Int64, ::Float64, ::typeof(GraphPlot.spring_layout), ::LightGraphs.SimpleGraphs.SimpleGraph{Int64}, ::Int64, ::Vector{Float64})@layout.jl:158
    top-level scope@Local: 2[inlined]

but works just fine when I omit the C=2.0. (I am setting the random seed as 97339.

hdavid16 commented 1 year ago

@SimonEnsemble this is fixed in PR #186. However, if you need to make plots where you set the seed, just use GraphMakie (since it is not clear if this PR will be merged at any point):

using Graphs, GraphMakie
using GraphMakie:Spring

graph = SimpleGraph(9)
add_edge!(graph, 1, 2)

add_edge!(graph, 3, 4)
add_edge!(graph, 3, 5)
add_edge!(graph, 4, 5)

add_edge!(graph, 6, 7)
add_edge!(graph, 7, 8)
add_edge!(graph, 8, 9)

graphplot(graph,layout=Spring(seed=97339,C=2.0))

image

If you just want the locations then run,

loc_x, loc_y = Spring(;C=2.0,seed=97339)(graph)

#9-element Vector{Point2{Float64}}:
# [6.085732446390886, 2.2914171065246642]
# [5.622402183255887, 1.0407819527554754]
# [1.5199667804243207, -8.373001182722112]
# [0.6096922018281642, -7.562980245682519]
# [2.013612134245273, -7.320345707757515]
# [-7.559104906247997, 5.12608753224364]
# [-6.192837350012484, 6.015599417248268]
# [-4.356427140564693, 6.320432042063193]
# [-2.8337673677147994, 6.923911898290827]