I developed the following code_1
which renders figure_1
.
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
from matplotlib.animation import FuncAnimation
import random
from itertools import count
from matplotlib import gridspec
edge_labels = [0.33, 0.55, 0.66]
fig = plt.figure(figsize=(10,7))
gs = gridspec.GridSpec(2, 2, height_ratios=[3,1])
# ------- Graph Part ---------------------------------------
ax = fig.add_subplot(gs[:-1,:])
G = nx.DiGraph()
l1, l2, l3, l4, l5, l6, l7 ='1', '2', '3', '4', '5', '6', '7'
edgelist= [(l1,l2),(l2,l3), (l3,l4), (l3,l5), (l4, l1), (l5, l6), (l5, l7)]
e1 = float("{:.3f}".format(edge_labels[0]))
e2 = float("{:.3f}".format(edge_labels[1]))
e3 = float("{:.3f}".format(edge_labels[2]))
e_labels = [e1, e2, e3]
edge_labels = {(l1,l2):e_labels[0], (l4,l1):e_labels[0], (l2,l3):e_labels[1], (l3,l5):e_labels[2], (l5,l7):e_labels[2]}
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_edge_labels(G,pos,edge_labels=edge_labels, font_size=15)
ax = nx.draw_networkx_labels(G, pos=pos, labels=dict(zip(pos.keys(),pos.keys())), font_color="black")
# ------- Lines Part -------------------------------------
ax2 = fig.add_subplot(gs[-1, :-1])
ax3 = fig.add_subplot(gs[-1, -1])
x_vals = []
y_vals = []
index = count()
def animate(i):
x_vals.append(next(index))
y_vals.append(random.random())
ax2.cla()
ax2.plot(x_vals, y_vals)
ax3.cla()
ax3.plot(x_vals, y_vals)
ani = FuncAnimation(fig, animate, interval=50, frames= 30, repeat=False)
# -------------------------------------------------------
fig.show()
figure 1
Then I tried to make the code in a function for dynamic usage, but it seems that I got something wrong because somehow, it didn't work as expected, the animated line-charts at the bottom didn't show. Here is code_2
for the function version of code_1
. I am suspecting it's in the axes declaration maybe but can't figure out where I am mistaken.
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
from matplotlib.animation import FuncAnimation
import random
from itertools import count
from matplotlib import gridspec
global ax, ax2, ax3, fig, gs
fig = plt.figure(figsize=(10,7))
gs = gridspec.GridSpec(2, 2, height_ratios=[3,1])
def render(edge_labels):
global ax, ax2, ax3, fig, gs
# ------- Graph Part ---------------------------------------
ax = fig.add_subplot(gs[:-1,:])
G = nx.DiGraph()
l1, l2, l3, l4, l5, l6, l7 ='1', '2', '3', '4', '5', '6', '7'
edgelist= [(l1,l2),(l2,l3), (l3,l4), (l3,l5), (l4, l1), (l5, l6), (l5, l7)]
e1 = float("{:.3f}".format(edge_labels[0]))
e2 = float("{:.3f}".format(edge_labels[1]))
e3 = float("{:.3f}".format(edge_labels[2]))
e_labels = [e1, e2, e3]
edge_labels = {(l1,l2):e_labels[0], (l4,l1):e_labels[0], (l2,l3):e_labels[1], (l3,l5):e_labels[2], (l5,l7):e_labels[2]}
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_edge_labels(G,pos,edge_labels=edge_labels, font_size=15)
ax = nx.draw_networkx_labels(G, pos=pos, labels=dict(zip(pos.keys(),pos.keys())), font_color="black")
# ------- Lines Part -------------------------------------
ax2 = fig.add_subplot(gs[-1, :-1])
ax3 = fig.add_subplot(gs[-1, -1])
x_vals = []
y_vals = []
index = count()
def animate(i):
global ax2, ax3, fig
x_vals.append(next(index))
y_vals.append(random.random())
ax2.cla()
ax2.plot(x_vals, y_vals)
ax3.cla()
ax3.plot(x_vals, y_vals)
ani = FuncAnimation(fig, animate, interval=50, frames= 30, repeat=False)
# -------------------------------------------------------
fig.show()
render([0.33, 0.55, 0.66])
I am using Python 3.6, networkx 2.4, matplotlib 3.3.3 with IDLE and Win10.