matplotlib的animation和networkx的相关问题

土肥宅娘口三三 2017-07-12 03:45:17
目的是使用python的matplotlib和networkx做一个pagerank算法的动态展示过程,对python完全是小白级别的。
现在的问题是,在动态变化的过程中,上一帧的图片还在,请教大神,要怎么去掉以前的图片。只展示最新的。
代码如下:

import networkx as nx
import matplotlib.pyplot as plt
from numpy import *
from matplotlib import animation

adjacent = []
edges = []
trM = []
v = []
node_size = []

def getNetwork():
input = open("adjacent.txt", "r")
for line in input.readlines():
one = []
line = line.strip().split(" ")
for x in range(len(line)):
one.append(int(line[x]))
adjacent.append(one)
input.close()
return adjacent
def adjacent2csv():
for i in range(len(adjacent)):
for j in range(len(adjacent[i])):
if adjacent[i][j] == 1:
edges.append((i+1, j+1))
return edges
def getTrMatrix():
a = array(adjacent)
b = transpose(a)
c = zeros((a.shape),dtype = float)
for i in range(a.shape[0]):
for j in range(a.shape[1]):
c[i][j] = a[i][j]/(b[j].sum())
return c
def init():
for i in range(len(adjacent[0])):
v[i] = float(1)/len(adjacent[0])
# nx.draw(G, pos=nx.shell_layout(G), node_color='w', node_size=[x * 40000 for x in v], with_labels = True)
return v
def animate(i):
global v
v = p*dot(trM,v) + (1-p)*v
nx.draw(G, pos=nx.shell_layout(G), node_color='w', node_size=[x * 40000 for x in v], with_labels = True)
return v

getNetwork()
adjacent2csv()
G = nx.DiGraph()
G.add_edges_from(edges)
layout = nx.spring_layout(G)
fig = plt.figure(figsize=(10, 10))
trM = getTrMatrix()
v = zeros((len(adjacent[0]),1),dtype = float)
p = 0.8
ani = animation.FuncAnimation(fig=fig, func=animate, frames=1000, init_func=init, interval=1000, repeat=False, blit=True)
plt.show()



就像图中的节点1和节点3的情况,因为这些节点是慢慢变小的,所以出现和很多圈圈,怎么去掉呢, 求大神指导!!
...全文
280 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 楼主 robin_Xu_shuai 的回复:
目的是使用python的matplotlib和networkx做一个pagerank算法的动态展示过程,对python完全是小白级别的。 现在的问题是,在动态变化的过程中,上一帧的图片还在,请教大神,要怎么去掉以前的图片。只展示最新的。 代码如下:

import networkx as nx
import matplotlib.pyplot as plt
from numpy import *
from matplotlib import animation

adjacent = []
edges = []
trM = []
v = []
node_size = []

def getNetwork():
    input = open("adjacent.txt", "r")
    for line in input.readlines():
        one = []
        line = line.strip().split(" ")
        for x in range(len(line)):
            one.append(int(line[x]))
        adjacent.append(one)
    input.close()
    return adjacent
def adjacent2csv():
    for i in range(len(adjacent)):
        for j in range(len(adjacent[i])):
            if adjacent[i][j] == 1:
                edges.append((i+1, j+1))
    return edges
def getTrMatrix():
    a = array(adjacent)
    b = transpose(a)
    c = zeros((a.shape),dtype = float)  
    for i in range(a.shape[0]):  
        for j in range(a.shape[1]):  
            c[i][j] = a[i][j]/(b[j].sum())
    return c
def init():
    for i in range(len(adjacent[0])):  
        v[i] = float(1)/len(adjacent[0])
    # nx.draw(G, pos=nx.shell_layout(G), node_color='w', node_size=[x * 40000 for x in v], with_labels = True)
    return v
def animate(i):
    global v
    v = p*dot(trM,v) + (1-p)*v
    nx.draw(G, pos=nx.shell_layout(G), node_color='w', node_size=[x * 40000 for x in v], with_labels = True)
    return v

getNetwork()
adjacent2csv()
G = nx.DiGraph()
G.add_edges_from(edges)
layout = nx.spring_layout(G)
fig = plt.figure(figsize=(10, 10))
trM = getTrMatrix()
v = zeros((len(adjacent[0]),1),dtype = float)
p = 0.8
ani = animation.FuncAnimation(fig=fig, func=animate, frames=1000, init_func=init, interval=1000, repeat=False, blit=True)
plt.show()
就像图中的节点1和节点3的情况,因为这些节点是慢慢变小的,所以出现和很多圈圈,怎么去掉呢, 求大神指导!!
ok

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧