简体   繁体   中英

Updating a matplotlib figure drawn by networkx

I want to plot a network that was made with the library networkx for python as such:

import networkx as nx
import matplotlib.pyplot as plt 
nx.draw(graph, with_labels=True, node_color=setColorMap(graph, nodeDict))

It works well however i need to update it every second and i tried using

plt.close()
nx.draw(graph, with_labels=True, node_color=setColorMap(graph, nodeDict))
plt.show()

but that did not seem to work. I have searched the web and found ways to close figures and update them on matplotlib but i wasn't able to do it with the networkx drawing. There might be a very easy solution to it, i am not excperienced with those libraries.

You should use the matplotlib animation functionality. Call the nx.draw() commands within the update function.

The basic sketch of this code would look like:

# do networkx stuff
fig = plt.figure()
def update(it):
    G = graph_list[it]
    nx.draw(G, with_labels=True, ...)
ani = animation.FuncAnimation(fig, update, frames=list(range(num_frames)))
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM