Animating Network Growth with NetworkX and Matplotlib

前端 未结 3 447
被撕碎了的回忆
被撕碎了的回忆 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

    An improved version of bretlance's. Hope it will be helpful. It will show animations but not picture after picture.

    Still don't know how the owner of the question Animate drawing networkx edges made use of matplotlib's animation

    #!/usr/bin/env python
    import random
    import pylab
    from matplotlib.pyplot import pause
    import networkx as nx
    pylab.ion()
    
    graph = nx.Graph()
    node_number = 0
    graph.add_node(node_number, Position=(random.randrange(0, 100), random.randrange(0, 100)))
    
    def get_fig():
        global node_number
        node_number += 1
        graph.add_node(node_number, Position=(random.randrange(0, 100), random.randrange(0, 100)))
        graph.add_edge(node_number, random.choice(graph.nodes()))
        nx.draw(graph, pos=nx.get_node_attributes(graph,'Position'))
    
    num_plots = 50;
    pylab.show()
    
    for i in range(num_plots):
    
        get_fig()
        pylab.draw()
        pause(2)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-04 19:38

    Upon review, that code wasn't nearly as relevant to this problem as I'd thought. However, I was able to use this SO answer and this SO answer to cobble together an answer for you. Here's code that will create a graph, add 50 random nodes and 50 random edges to it, and display an image of the graph after every node and edge is added. A few of the key changes from your code:

    1. This code is based on using pylab.ion() (see here for more info).
    2. Your code tries to use one figure and change the image within it. This code creates a new figure for each frame.
    3. This code doesn't write out to .mp4. If you really need to do that, I would suggest writing the figures out to .png file while rendering them, and converting to mp4 after the fact.
    4. Note that this code uses matplotlib.pyplot.pause() instead of time.sleep(). The figures won't render if you use time.sleep().

    Good luck!

    import random
    import pylab
    from matplotlib.pyplot import pause
    import networkx as nx
    pylab.ion()
    
    graph = nx.Graph()
    node_number = 0
    graph.add_node(node_number, Position=(random.randrange(0, 100), random.randrange(0, 100)))
    
    def get_fig():
        global node_number
        node_number += 1
        graph.add_node(node_number, Position=(random.randrange(0, 100), random.randrange(0, 100)))
        graph.add_edge(node_number, random.choice(graph.nodes()))
        fig = pylab.figure()
        nx.draw(graph, pos=nx.get_node_attributes(graph,'Position'))
        return fig
    
    num_plots = 50;
    pylab.show()
    
    for i in range(num_plots):
    
        fig = get_fig()
        fig.canvas.draw()
        pylab.draw()
        pause(2)
        pylab.close(fig)
    
    0 讨论(0)
提交回复
热议问题