python - 使用更新的数据在同一图中动画节点颜色
问题描述
我使用节点和边缘文件创建了一个网络图。使用另一个代码每 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()
解决方案
推荐阅读
- uwp - UWP DataGridComboBoxColumn 在选择更改时更改行值
- php - PHP / MySQL - 复选框值返回 0 和 1
- r - 动态访问空列表或附加到现有列表
- sql - 基于 ERD 创建 ERD 和 SQL 表
- mysql - 如何获取按日期排序的行的出现次数
- angular - navigator.clipboard.readText() 的等效代码是什么?
- .net - 在实体框架模型中将数据插入 SQL Server
- android - 为什么Intent中的类有“::class.java”后缀?
- c++ - 使用 push-relabel 计算最大流量时获取最小切割
- ios - 如何将 YUV 数据转换为 CVPixelBufferRef?