From: timv Date: Fri, 7 Jun 2013 22:18:15 +0000 (-0400) Subject: added post-processing script for animated force-directed graph layout. X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=2e579263187df554dcfdfc0f322be2c71d6d30e7;p=dyna2 added post-processing script for animated force-directed graph layout. --- diff --git a/src/Dyna/Backend/Python/graph.py b/src/Dyna/Backend/Python/graph.py new file mode 100644 index 0000000..c6d01de --- /dev/null +++ b/src/Dyna/Backend/Python/graph.py @@ -0,0 +1,39 @@ +import pylab as pl +from matplotlib.animation import FuncAnimation +from collections import defaultdict + +def g(nodes, edges, t, ax, interp): + ax.cla() + ax.set_title(t) + ax.set_xlim(-2,2) + ax.set_ylim(-2,2) + + pos = defaultdict(lambda: (0,0)) + pos.update({node: p for _, (node, _, p) in interp.chart['pos/2'][:,t,:]}) + + for u,v in edges: + if u < v: + (a,b), (c,d) = pos[u], pos[v] + ax.plot([a,c], [b,d]) + + for s in nodes: + x,y = pos[s] + ax.text(x,y,s) + +def animate(interp): + [(_, (niter,))] = interp.chart['niter/0'][:,] + + nodes = [name for _, (name, _) in interp.chart['node/1'][:,:]] + edges = [(u,v) for _, (u,v,_) in interp.chart['edge/2'][:,:,:]] + + fig = pl.figure() + ax = pl.axes() + + print 'creating animation..' + anim = FuncAnimation(fig, lambda t: g(nodes, edges, t % niter, ax, interp), frames=niter*3) + print 'saving...' + anim.save('examples/force.dyna.mp4', fps=30, extra_args=['-vcodec', 'libx264']) + print 'wrote examples/force.dyna.mp4' + + +animate(interp)