Compose GraphPlot LightGraphs Position

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.

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)

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

1 Like

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

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

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…

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.

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:

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

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

The best I can get is this:

anim_test

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 ¯\(ツ):slight_smile: )

2 Likes

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

1 Like

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

1 Like