the code below runs a function (showNet) 100 times. The function takes in a list of node-labels to update the network figure. Now, the code takes about 16.55 sec to complete, meaning that each of the 100 frames/figures-updates took 16.55 / 100 = 0.16 sec. Which is less than my needs.
My question is, is there any way I can make this much faster? I am aware that there are some solutions like multiprocessing or blit, but I honestly don't know how to use them in matplotlib.
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import random
import time
global ax, fig, G
fig, ax = plt.subplots(figsize=(10,7))
G = nx.DiGraph()
def showNet(n):
global ax, fig, G
plt.style.use('Solarize_Light2')
fig.clf()
l1, l2, l3, l4, l5, l6, l7 = n[0], n[1], n[2], n[3], n[4], n[5], n[6]
edgelist= [(l1,l2),(l2,l3), (l3,l4), (l3,l5), (l4, l1), (l5, l6), (l5, l7)]
pos = {l1: (10, 60), l2: (10, 40), l3:(80, 40), l4:(140, 60), l5:(200, 20), l6:(250, 40), l7:(250,10)}
ax = nx.draw_networkx_nodes(G, pos=pos, nodelist=list(pos.keys()), node_size=2000, alpha=1)
ax = nx.draw_networkx_edges(G, pos=pos, edgelist= edgelist , edge_color="gray", arrows=True, arrowsize=10, arrowstyle='wedge')
ax = nx.draw_networkx_labels(G, pos=pos, labels=dict(zip(pos.keys(),pos.keys())), font_color="black")
# Hide grid lines
plt.grid(False)
plt.ion()
fig.show()
plt.pause(0.0000001)
start = time.perf_counter()
for i in range(100):
n = random.sample(range(1, 10), 7)
showNet(n)
finish = time.perf_counter()
print(finish - start)