Skip to content

Instantly share code, notes, and snippets.

@com3345
Created September 12, 2016 00:20
Show Gist options
  • Save com3345/eb6c6bca37b070a4a11979a5e39917c9 to your computer and use it in GitHub Desktop.
Save com3345/eb6c6bca37b070a4a11979a5e39917c9 to your computer and use it in GitHub Desktop.
graph drawing animation
import matplotlib.pyplot as plt
from matplotlib import animation
import networkx as nx
import os
import time
os.chdir('/Users/Cosann/Downloads/initial_graph_gml')
filenames = os.listdir('.')
filename = filenames[0]
lines = open(filenames[1]).readlines()
graph = nx.read_gml(filename)
pos = nx.spring_layout(graph)
fig = plt.figure(figsize=(10, 10))
plt.axis('off')
nx.draw_networkx_nodes(graph, pos=pos, node_size=30)
nx.draw_networkx_edges(graph, pos=pos, arrows=False, alpha=0.2)
start_time = time.time()
ax = fig.add_subplot(111)
time_template = 'time = %.5fs'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
j = 0
new_edges = []
def update(i):
global j, new_edges, speed
s, t, d_t = lines[j].split(' ')
d_t = float(d_t.strip())
delta_time = time.time() - start_time
time_text.set_text(time_template % (delta_time))
if delta_time > d_t:
graph.add_edge(s, t)
nx.draw_networkx_edges(graph, pos=pos, edgelist=[(s, t)], width=2, arrows=False, edge_color="b")
j += 1
# if (j + 1) % 3 == 0:
# plt.clf()
# plt.axis('off')
# nx.draw_networkx_nodes(graph, pos=pos, node_size=40)
# nx.draw_networkx_edges(graph, pos=pos, arrows=False, alpha=0.2)
anim = animation.FuncAnimation(fig, update)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment