突出显示NetworkX中的某些节点/边缘-使用zip()的问题 [英] Highlighting certain nodes/edges in NetworkX - Issues with using zip()

查看:76
本文介绍了突出显示NetworkX中的某些节点/边缘-使用zip()的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用networkx填充网络图.我的问题是当我要突出显示图形无法生成的路径(例如最短路径)时,它将在下面返回错误.

I able to populate network graph using networkx. My problem is when I want to highlight the path (shortest path for example) the graph cannot generate and it will return error below.

nx.draw_networkx_edges(Gr,pos,edgelist=path_edges,edge_color='r',width=10)
  File "/usr/local/lib/python3.6/site-packages/networkx/drawing/nx_pylab.py", line 578, in draw_networkx_edges
    if not edgelist or len(edgelist) == 0:  # no edges!
TypeError: object of type 'zip' has no len()

我正在寻找解决方案,这是因为脚本正在python3上运行,并且由于该错误.解决方案之一是按如下所示更改和添加列表.

I look for solution and this is because the script is running over python3 and due to that I get this error. One of the solution is to change and add list as below.

原始:

Gr = nx.DiGraph() 
edges = graph
Gr.add_edges_from(edges)
pos = nx.spring_layout(Gr)

path = nx.shortest_path(Gr,source=1,target=7)
path_edges = zip(path,path[1:])
nx.draw_networkx_nodes(Gr,pos,nodelist=path,node_color='r')
nx.draw_networkx_edges(Gr,pos,edgelist=path_edges,edge_color='r',width=10)
plt.axis('equal')
plt.show()

已修改:

path = nx.shortest_path(Gr,source=1,target=7)
path_edges = list(zip(path,path[1:]))
nx.draw_networkx_nodes(Gr,pos,nodelist=path,node_color='r')
nx.draw_networkx_edges(Gr,pos,edgelist=path_edges,edge_color='r',width=10)
plt.axis('equal')
plt.show()

能够运行脚本而没有错误,并且能够生成图形,但是突出显示的路径(红色)未与拓扑节点和链接对齐.红色路径应在顶部,并对准节点/路径1-2-3-4-5-6-7.请参考下面的图片

Able to run the script no error and able to generate graph but the highlighted path (red) is not aligned to the topology node and link. The red path should be on-top and align of node/path 1-2-3-4-5-6-7. Please refer to images below

请告知如何解决此问题.

Please advise how to resolve this problem.

推荐答案

我能够使用以下代码生成您似乎想要的图形-如果您遇到任何问题,请告诉我.您是正确的,您需要将 zip 对象转换为 list ,但是我认为您的图形代码中可能还存在其他错误.如果您需要每次 nx.spring_layout 的输出都相同,则可以使用 seed 关键字参数,例如 pos = nx.spring_layout(Gr,seed = 123).

I was able to generate the graph you appear to be after with the following code - let me know if you encounter any issues. You were correct that you need to convert the zip object to a list, but I think there may be other mistakes in your drawing code. If you need the output from nx.spring_layout to be the same every time, you can use the seed keyword argument, for example pos = nx.spring_layout(Gr, seed=123).

代码:

import networkx as nx

# Set up graph
Gr = nx.DiGraph() 
edges = [(i+1, i+2) for i in range(10)] + [(i+2, i+1) for i in range(10)]
Gr.add_edges_from(edges)

# Get position using spring layout
pos = nx.spring_layout(Gr)

# Get shortest path
path = nx.shortest_path(Gr,source=1,target=7)
path_edges = list(zip(path,path[1:]))

# Draw nodes and edges not included in path
nx.draw_networkx_nodes(Gr, pos, nodelist=set(Gr.nodes)-set(path))
nx.draw_networkx_edges(Gr, pos, edgelist=set(Gr.edges)-set(path_edges), connectionstyle='arc3, rad = 0.3')

# Draw nodes and edges included in path
nx.draw_networkx_nodes(Gr, pos, nodelist=path, node_color='r')
nx.draw_networkx_edges(Gr,pos,edgelist=path_edges,edge_color='r', connectionstyle='arc3, rad = 0.3')

# Draw labels
nx.draw_networkx_labels(Gr,pos)

输出:

这篇关于突出显示NetworkX中的某些节点/边缘-使用zip()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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