0

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

enter image description here

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])

figure 2 enter image description here

I am using Python 3.6, networkx 2.4, matplotlib 3.3.3 with IDLE and Win10.

Mr. T
  • 11,960
  • 10
  • 32
  • 54
mac179
  • 1,540
  • 1
  • 14
  • 24
  • I cannot reproduce this problem. The animation in code 2 runs as expected. matplotlib 3.3.3, Python 3.8. First thing to check - restart. – Mr. T Feb 28 '21 at 09:05
  • I am running the animation from code 1 with no errors, What do you get as error. I am using Python 3.6, networkx== 2.4, matplotlib==3.3.3 – mac179 Feb 28 '21 at 10:16
  • I am actually using IDLE, the default Python IDE and windows 10 as operating system – mac179 Feb 28 '21 at 10:27
  • if you want to reproduce the code_1 in colab: 1- comment fig.show() 2- write this line : ani.save('animation.gif', fps=30) 3- and you will have the animation shown above – mac179 Feb 28 '21 at 10:35
  • As I said, both codes work equally well in my environment. It is also difficult to understand why the changes between code 1 and 2 would influence the interactive behavior in any setting. – Mr. T Feb 28 '21 at 10:44
  • sorry I don't understand, do you mean that in you environment, code 2 produces the wanted animation ?? or not – mac179 Feb 28 '21 at 11:13
  • Okey ! as I said, I am using Python 3.6, networkx== 2.4, matplotlib==3.3.3, IDLE and windows 10. is there anything else concerning the environment ? – mac179 Feb 28 '21 at 11:43
  • No need to repeat this. I even copied this information into your question. My last comment here: Check if the problem is backend-dependent. – Mr. T Feb 28 '21 at 11:50

0 Answers0