Animating Network Growth with NetworkX and Matplotlib

前端 未结 3 453
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 18:50

I would like to animate a graph that grows over time.

This is what I have so far:

fig = plt.figure()
ims = []
graph = nx.Graph()
for i in range(50):
         


        
3条回答
  •  情话喂你
    2020-12-04 19:16

    As the existing answers are quite old, here is a version of how the code could look like with current versions of matplotlib and networkx.

    import random
    import networkx as nx
    import matplotlib.pyplot as plt
    
    graph = nx.Graph()
    num_plots = 50
    for node_number in range(num_plots):
        graph.add_node(node_number, Position=(random.randrange(0, 100), random.randrange(0, 100)))
        graph.add_edge(node_number, random.choice(list(graph.nodes())))
        nx.draw(graph, pos=nx.get_node_attributes(graph,'Position'))
        plt.pause(0.5)
    

提交回复
热议问题