首页 > 解决方案 > 使用更新的数据在同一图中动画节点颜色

问题描述

我使用节点和边缘文件创建了一个网络图。使用另一个代码每 10 秒更改一次节点文件,并且边缘文件是静态的。用于使用带有绘图代码的更新的 I 带客户端套接字代码进行绘图。

但问题是每当服务器向客户端发送更新的文件时,就会显示新的绘图图。这就是为什么在我的屏幕上显示 N 个绘图图的原因。我想像这样根据节点状态在同一图中只动画节点​​颜色。 什么工具可以画出动画网络图

客户端代码 -

if len(text2) < 30:
            if CL[0] == "get":
                for i in range(20):
                    BigC = open("Received-" + CL[1], "wb")
                    d = 0
                    try:
                            # number of paclets
                        CountC, countaddress = s.recvfrom(4096)
                    except ConnectionResetError:
                        print(
                            "Error. Port numbers not matching.")
                        sys.exit()
                    except:
                        print("Timeout or some other error")
                        sys.exit()

                    tillC = CountC.decode('utf8')
                    tillCC = int(tillC)
                    #print("Receiving packets will start now if file exists.")
                    # print(
                    #   "Timeout is 15 seconds so please wait for timeout at the end.")
                    while tillCC != 0:
                        ClientBData, clientbAddr = s.recvfrom(4096)
                        dataS = BigC.write(ClientBData)
                        d += 1
                        print("Received packet number:" + str(d))
                        tillCC = tillCC - 1

                    #BigC.close()
                    print(
                        "Check contents in your directory.")
                    os.system('python3 plotting.py &')
                time.sleep(3)
            

绘图代码 -

fig = plt.figure()
net = fig.add_subplot(111)

def init_func():
    with open("fileName", 'r') as nodecsv:
        nodereader = csv.reader(nodecsv)
        nodes = [n for n in nodereader][1:]
        node_names = [n[0] for n in nodes]

    with open("fileName", 'r') as edgecsv:
        edgereader = csv.reader(edgecsv)
        edges = [tuple(e) for e in edgereader][1:]

    g = nx.Graph()
    g.add_nodes_from(node_names)
    g.add_edges_from(edges)
    
    node_status = {}
    for node in nodes:
        if random.choice([True, False]):
            node_status[node[0]] = node[1]
        else:
            node_status[node[0]] = 'R'
    nx.set_node_attributes(g, node_status, 'node_status')
********** other coding***********

    plt.clf()
    plt.cla()
    ax = plt.gca()
    ax.set_title('Graph')
    global pos
    pos = nx.fruchterman_reingold_layout(g)
    nx.draw(g, pos=pos, node_size=node_size, node_color=color_map, linewidths=2, **options)

def update(it):
***************Same as above**************
    nx.draw(g, pos=pos, node_size=node_size, node_color=color_map, linewidths=2, **options)
ani = animation.FuncAnimation(fig, update, init_func=init_func ,interval=1000)
plt.show()

标签: pythonmatplotlibanimationnetworkxmatplotlib-animation

解决方案


推荐阅读