Python Forum
Animate graph nodes inside a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Animate graph nodes inside a function
#1
I have written the following code in Jupyter notebook to animate the colours of the graph nodes.

%matplotlib notebook

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation
from functools import partial

def update(i,G,pos,t,ax):
    #fig.clf()    
    cmap = matplotlib.cm.get_cmap('Blues')
    color_map = []
    for node in G:
        color_map.append(cmap(t[i]))
        
    nx.draw_networkx_nodes(G,pos,node_color=color_map,ax=ax)

t = np.linspace(0,1,10)
G = nx.petersen_graph()
pos = nx.spring_layout(G) # positions for all nodes   

fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
#fig.add_axes(rect,label=label1)
plt.axis('off')

upd = partial(update,G=G,pos=pos,t=t,ax=ax)
nx.draw_networkx_edges(G,pos,width=1.0,alpha=0.5,ax=ax) #nodelist=[0,1,2,3], #node_color='r', #node_size=500,
    
ani = matplotlib.animation.FuncAnimation(fig,upd,repeat=True,interval=100,frames=len(t),repeat_delay=10)
plt.show()
Although this works in my case, I would like to be able to execute the whole program inside a function like so

%matplotlib notebook
    
    import networkx as nx
    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.animation
    from functools import partial
    
    def update(i,G,pos,t,ax):
        #fig.clf()    
        cmap = matplotlib.cm.get_cmap('Blues')
        color_map = []
        for node in G:
            color_map.append(cmap(t[i]))
            
        nx.draw_networkx_nodes(G,pos,node_color=color_map,ax=ax)
    
    def anim():
        t = np.linspace(0,1,10)
        G = nx.petersen_graph()
        pos = nx.spring_layout(G) # positions for all nodes   
    
        fig = plt.figure()
        ax = fig.add_axes([0,0,1,1])
        #fig.add_axes(rect,label=label1)
        plt.axis('off')
    
        upd = partial(update,G=G,pos=pos,t=t,ax=ax)
        nx.draw_networkx_edges(G,pos,width=1.0,alpha=0.5,ax=ax) #nodelist=[0,1,2,3], #node_color='r', #node_size=500,
        
        ani = matplotlib.animation.FuncAnimation(fig,upd,repeat=True,interval=100,frames=len(t),repeat_delay=10)
        plt.show()
        
    anim() 
This one, however, does not work. Could someone tell me why?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I rearrange df as the nodes index in pytorch geometric manner? uqlsmey 0 492 Jul-31-2023, 11:28 AM
Last Post: uqlsmey
  with open context inside of a recursive function billykid999 1 549 May-23-2023, 02:37 AM
Last Post: deanhystad
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 752 May-02-2023, 08:40 AM
Last Post: Gribouillis
  Animate the midpoints of a 3d “Rhombic dodecahedron” Beck_Johnson 0 839 Nov-05-2022, 04:18 PM
Last Post: Beck_Johnson
  function accepts infinite parameters and returns a graph with those values edencthompson 0 812 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  How to make global list inside function CHANKC 6 2,978 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  Parameters aren't seen inside function Sancho_Pansa 8 2,814 Oct-27-2020, 07:52 AM
Last Post: Sancho_Pansa
  Python3 binary tree not replacing parent nodes with child nodes Aspect11 0 1,741 Sep-23-2020, 02:22 PM
Last Post: Aspect11
  read individual nodes from an xml url using pandas mattkaplan27 5 2,869 Jul-05-2020, 10:06 PM
Last Post: snippsat
  Modifying anytree Nodes gw1500se 1 2,610 Jun-05-2020, 03:44 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020