# Compose GraphPlot LightGraphs Position

**URL:** https://discourse.julialang.org/t/compose-graphplot-lightgraphs-position/33177
**Category:** Visualization
**Tags:** question
**Created:** [January 10, 2020, 12:41am UTC](https://discourse.julialang.org/t/compose-graphplot-lightgraphs-position/33177 "2020-01-10T00:41:32Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Simplex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simplex/32/12188_2.png) [@Simplex](https://discourse.julialang.org/u/Simplex)
#### Post date: [January 10, 2020, 12:41am UTC](https://discourse.julialang.org/t/compose-graphplot-lightgraphs-position/33177/1 "2020-01-10T00:41:32Z")

</div>

I do a project about infection spreading. I want to have my network immovable but I don’t find a solution.  
I also tried to have no superposition but I find nothing

Thanks a lot for your help !

My code won’t work with you because you need all my work to make it work but here is the main code for the representation.

```julia
affinite=deepcopy(affinitebase)
etat=deepcopy(etatbase)
jourMalade=deepcopy(jourMaladebase)
limnMalade=2;
resultat=zeros(nJours+1,4)
resultat[1,:]=results(etat)

FirstTime=now()
etattot=zeros(nJours+1,length(etat))
etattot[1,:]=etat;

set_default_graphic_size(20cm, 15cm)
anim=Animation()
Network = Graph(affinite)
nvertices = nv(Network) # number of vertices
nedges = ne(Network) # number of edges
nodecolor = [colorant"lightseagreen", colorant"red", colorant"orange", colorant"blue"] #["Susceptibles","Infectées","Soignées","Vaccinées"]

for j in 1:nJours
    etatTest=etat;
    for i in 1:length(etat)
        transmission(i,etat,affinite,limnMalade,incubation,amisbase)
    end
    
    nodefillc = nodecolor[etat.+1]
    layout=(args...)->spring_layout(args...; C=20)
    p=gplot(Network,layout=spring_layout,nodelabel=1:nvertices,nodefillc=nodefillc)
    output = compose(p,
        (context(), Compose.text(0, -1, "Représentation du réseau $("aléatoire") jour $(j)", hcenter, vcenter), fontsize(24pt)),
        (context(), Compose.text(-1, -0.8, "Vacciné si $(limnMalade) amis malades"), fontsize(10pt)),
        (context(), circle([-1], [1-3*0.15,1-2*0.15,1-0.15,1], [0.05]), fill(nodecolor)), #Légende
        (context(), Compose.text([-1+0.07], [1.04-3*0.15,1.04-2*0.15,1.04-0.15,1.04], ["Susceptible","Infecté","Soigné","Vacciné"])), #Légende
        (context(), rectangle(-10,-10,20,20), fill("white"), Compose.stroke("black")))
    j=length(anim.frames) + 1
    tmpfilename=joinpath(anim.dir,@sprintf("%06d.png",j))
    Compose.draw(PNG(tmpfilename),output)
    push!(anim.frames, tmpfilename)

    resultat[j+1,:]=results(etat);
end

LastTime=now();

gif(anim, "Infection$(nJours).gif", fps = 1)

```

 ![Infection50](https://global.discourse-cdn.com/julialang/original/3X/3/2/325bbb2ba4b370aafedde25b65feecfc721f4496.gif)

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [January 10, 2020, 7:55am UTC](https://discourse.julialang.org/t/compose-graphplot-lightgraphs-position/33177/2 "2020-01-10T07:55:09Z")

</div>

Randomized start locations [https://github.com/JuliaGraphs/GraphPlot.jl/blob/6458908ac5803efd87428b1fd1911dcd93d5e381/src/layout.jl#L105](https://github.com/JuliaGraphs/GraphPlot.jl/blob/6458908ac5803efd87428b1fd1911dcd93d5e381/src/layout.jl#L105) ?

---

<div class="post-metadata">

### Author: ![Simplex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simplex/32/12188_2.png) [@Simplex](https://discourse.julialang.org/u/Simplex)
#### Post date: [January 10, 2020, 10:55am UTC](https://discourse.julialang.org/t/compose-graphplot-lightgraphs-position/33177/3 "2020-01-10T10:55:52Z")

</div>

I don’t see how I can solve my problem with this ? Do I have to modify the function ?  
Thanks for your help !

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [January 10, 2020, 11:07am UTC](https://discourse.julialang.org/t/compose-graphplot-lightgraphs-position/33177/4 "2020-01-10T11:07:43Z")

</div>

Have you tried passing some less random values for the parameter `locs_x` and `locs_y`:

```julia
function spring_layout(g::AbstractGraph,
   locs_x=2*rand(nv(g)).-1.0,
   locs_y=2*rand(nv(g)).-1.0;

```

The README suggests making an anonymous function to do this…

---

<div class="post-metadata">

### Author: ![Simplex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simplex/32/12188_2.png) [@Simplex](https://discourse.julialang.org/u/Simplex)
#### Post date: [January 10, 2020, 11:36am UTC](https://discourse.julialang.org/t/compose-graphplot-lightgraphs-position/33177/5 "2020-01-10T11:36:46Z")

</div>

What I don’t understand is how to use locs\_x and locs\_y when they are defined.

gplot(g,layout=spring\_layout) ?  
Can I write my own function test and then use gplot(g,layout=test) ?  
I’ve tried but I don’t get it.

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [January 10, 2020, 11:54am UTC](https://discourse.julialang.org/t/compose-graphplot-lightgraphs-position/33177/6 "2020-01-10T11:54:32Z")

</div>

Perhaps you could make a very simple example that exhibits the behaviour you want to change, and post that as an issue at the Github repository? It’s possible the package authors are more likely to see it and be able to help you.

You could also try to seed the random numbers first, so that the same initial locations are used? Or investigate passing a layout to the function, as shown in the README:

```julia
layout=(args...) - >spring_layout(args...; C=20)

gplot(g, layout=layout, nodelabel=nodelabel)

```

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [January 10, 2020, 6:34pm UTC](https://discourse.julialang.org/t/compose-graphplot-lightgraphs-position/33177/7 "2020-01-10T18:34:00Z")

</div>

The best I can get is this:

![anim_test](https://global.discourse-cdn.com/julialang/original/3X/5/e/5e81194dbe0695b66b727664b0920e737a6498b8.gif)

```julia
using Plots, GraphPlot, LightGraphs, Compose, Measures, Printf
import Cairo
anim=Animation()
g = graphfamous("karate")
l_x = 2 * rand(nv(g)) .- 1.0
l_y = 2 * rand(nv(g)) .- 1.0
for i in 1:10
    mylayout(g, kws...) = spring_layout(g, l_x, l_y, kws...)
    p=gplot(g, layout=mylayout, 
        nodelabel = 1:nv(g))
    output = compose(p,
        (context(), Compose.text(1, 1, "Julia")),
        (context(), rectangle(), fill("white")))
    j=length(anim.frames) + 1
    tmpfilename=joinpath(anim.dir,@sprintf("%06d.png",j))
    Compose.draw(PNG(tmpfilename),output)
    push!(anim.frames, tmpfilename)
end
gif(anim, "anim_test.gif", fps = 5)

```

I’m not sure whether the algorithm here should always return the exact same layout, given the same input…? Ask the graphing folks on Slack or Github perhaps… what I know about graphs can be printed on the back of a t-shirt, so ¯\_(ツ)_/¯ 🙂 )

---

<div class="post-metadata">

### Author: ![Simplex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simplex/32/12188_2.png) [@Simplex](https://discourse.julialang.org/u/Simplex)
#### Post date: [January 10, 2020, 8:32pm UTC](https://discourse.julialang.org/t/compose-graphplot-lightgraphs-position/33177/8 "2020-01-10T20:32:26Z")

</div>

It’s perfect to make it immovable but my network is on the writings…  
Thanks for your help !

---

<div class="post-metadata">

### Author: ![AlexAuragan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexauragan/32/20313_2.png) [@AlexAuragan](https://discourse.julialang.org/u/AlexAuragan)
#### Post date: [December 16, 2020, 10:41am UTC](https://discourse.julialang.org/t/compose-graphplot-lightgraphs-position/33177/9 "2020-12-16T10:41:50Z")

</div>

I’ve got the same subjet as you and I did it like this (I hope is not too late)  
`loc_x, loc_y = spring_layout(g,MAXITER=200,C=1.)` before my for loop and `p=gplot(g, loc_x, loc_y, nodefillc=nodefillc)` inside. I used cormullion’s code as a base so everything else is pretty much the same. With that, the layout is static !

as I understand, mylaout is a function that helps gplot to calculate those loc\_x and loc\_y but you can just get rid of it and give it directly loc\_x and loc\_y previously calculated
