结合使用NetworkX和matplotlib.ArtistAnimation [英] Using NetworkX with matplotlib.ArtistAnimation

查看:122
本文介绍了结合使用NetworkX和matplotlib.ArtistAnimation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是创建一个动画,其中图形的节点随时间改变颜色.当我在matplotlib中搜索有关动画的信息时,通常会看到类似如下的示例:

What I want to do is create an animation in which the nodes of a graph change color with time. When I search for information on animation in matplotlib, I usually see examples that look something like this:

#!/usr/bin/python

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation

fig = plt.figure(figsize=(8,8))
images = []
for i in range(10):
  data = np.random.random(100).reshape(10,10)
  imgplot = plt.imshow(data)
  images.append([imgplot])
anim = ArtistAnimation(fig, images, interval=50, blit=True)
anim.save('this-one-works.mp4')
plt.show()

所以我想我可以做这样的事情:

So I thought I could just do something like this:

#!/usr/bin/python

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

G = nx.Graph()
G.add_edges_from([(0,1),(1,2),(2,0)])
fig = plt.figure(figsize=(8,8))
pos=nx.graphviz_layout(G)
images = []
for i in range(10):
  nc = np.random.random(3)
  imgplot = nx.draw(G,pos,with_labels=False,node_color=nc) # this doesn't work
  images.append([imgplot])
anim = ArtistAnimation(fig, images, interval=50, blit=True)
anim.save('not-this-one.mp4')
plt.show()

我要坚持的是如何使用nx.draw()绘制图形之后,如何获得适当类型的对象以放入要传递给ArtistAnimation的数组中.在第一个示例中,plt.imshow()返回类型为matplot.image.AxesImage的对象,但是nx.draw()实际上不返回任何内容.有什么办法可以使我适应合适的图像对象?

What I'm stuck on is how, after drawing the graph using nx.draw(), I can get an object of the appropriate type to put in the array being passed to ArtistAnimation. In the first example, plt.imshow() returns an object of type matplot.image.AxesImage, but nx.draw() doesn't actually return anything. Is there a way that I can get my hands on a suitable image object?

当然,完全可以接受完全不同的方法(在matplotlib中似乎总是有很多不同的方法来做同样的事情),只要我可以在完成后将动画另存为mp4.

Completely different approaches are welcome, of course (it seems like there's always many different ways to do the same thing in matplotlib), as long as I can save my animation as an mp4 when I'm done.

谢谢!

-craig

推荐答案

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

G = nx.Graph()
G.add_edges_from([(0,1),(1,2),(2,0)])
fig = plt.figure(figsize=(8,8))
pos=nx.graphviz_layout(G)
nc = np.random.random(3)
nodes = nx.draw_networkx_nodes(G,pos,node_color=nc)
edges = nx.draw_networkx_edges(G,pos) 


def update(n):
  nc = np.random.random(3)
  nodes.set_array(nc)
  return nodes,

anim = FuncAnimation(fig, update, interval=50, blit=True)

nx.draw不返回任何内容,因此为什么您的方法不起作用.最简单的方法是使用nx.draw_networkx_nodesnx.draw_networkx_edges绘制nodesedges,这将返回PatchCollectionLineCollection对象.然后可以使用set_array更新节点的颜色.

nx.draw does not return anything, hence why your method didn't work. The easiest way to do this is to draw the nodes and edges using nx.draw_networkx_nodes and nx.draw_networkx_edges which return PatchCollection and LineCollection objects. You can then update the color of the nodes using set_array.

使用相同的常规框架,您还可以移动节点(通过set_offsets用于PatchCollectionset_vertsset_segments用于LineCollection)

Using the same general frame work you can also move the nodes around (via set_offsets for the PatchCollection and set_verts or set_segments for LineCollection)

我见过的最佳动画教程: http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

best animation tutorial I have seen: http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

这篇关于结合使用NetworkX和matplotlib.ArtistAnimation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆