Highlighting certain nodes/edges in NetworkX - Issues with using zip()

◇◆丶佛笑我妖孽 提交于 2021-01-28 03:59:46

问题


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()

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.

original:

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()

Modified:

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()

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.


回答1:


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).

Code:

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)

Output:



来源:https://stackoverflow.com/questions/60164893/highlighting-certain-nodes-edges-in-networkx-issues-with-using-zip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!