Hi, I want to plot animation of the graph in which the width of every edge changes over time but the node location is given.
Here is the code generating one plot, given a simple graph G, the location dictionary of every node and the width dictionary of every edge.
function PlotGraphWithLoc(p, G::Graph, Loc::Dict, Width::Dict=Dict())
#return a plot that plots the graph with loc
x = Float64[]
y = Float64[]
if Width != Dict()
width_max = maximum(values(Width))
for e in edges(G)
u,v = src(e), dst(e)
push!(x, Loc[u][1])
push!(x, Loc[v][1])
push!(x, NaN)
push!(y, Loc[u][2])
push!(y, Loc[v][2])
push!(y, NaN)
plot!([Loc[u][1],Loc[v][1]],[Loc[u][2],Loc[v][2]],linecolor =:black, lw = Width[(u,v)]/width_max*3,label=nothing)
end
else
for e in edges(G)
u,v = src(e), dst(e)
push!(x, Loc[u][1])
push!(x, Loc[v][1])
push!(x, NaN)
push!(y, Loc[u][2])
push!(y, Loc[v][2])
push!(y, NaN)
plot!([Loc[u][1],Loc[v][1]],[Loc[u][2],Loc[v][2]],linecolor =:black, lw = 1,label= nothing)
end
end
(x, y)
end
One plot works fine, though not super fast (maybe it can be faster?). Takes about 0.6 s for plotting with this code to run.
N_x = 100
N_y = 10
N = N_x*N_y
xs = [fld(i,N_y) for i in 0:N-1]
ys = [rem(i,N_y) for i in 0:N-1]
A = zeros(N,N)
for i in 1:N, j in 1:N
abs(xs[i]-xs[j])+abs(ys[i]-ys[j])== 1 ? A[i,j]=1 : nothing
end
G = Graph(A)
Locs=Dict([(i,[xs[i],ys[i]]) for i in 1:N]);
p=Plots.plot()
@time x_g,y_g = PlotGraphWithLoc(p,G,Locs)
scatter!([Locs[i][1] for i in 1:N_y],[Locs[i][2] for i in 1:N_y], label = nothing)
scatter!([Locs[i][1] for i in N-N_y+1:N],[Locs[i][2] for i in N-N_y+1:N], label = nothing)
However, if I want to animate this with time-dependent width dictionary, it becomes super slow. 10 frames take 25 seconds and 100 frames take 240 s.
I wonder if I did something wrong to make it super slow? Or if there is a better way to animate this?