Networkx-使用节点列表绘制节点时出现问题 [英] Networkx - Problem with drawing nodes with nodelist
本文介绍了Networkx-使用节点列表绘制节点时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的嵌套元组:
i have an nested tuple like this:
mostfrequent = (('16.37.97.17', '178.237.19.228', '55177', '443', '6', '1', '46'), ('16.37.97.17', '178.237.17.97', '44492', '443', '6', '1', '46'), ('16.37.97.29', '178.237.17.61', '56326', '443', '6', '1', '46'), ('16.37.97.222', '104.131.44.62', '60179', '80', '6', '2', '620'), ('16.37.93.196', '16.37.157.74', '2049', '691', '6', '1', '100'))
我想使用通过节点列表声明的networkx绘制每个子元组的每个元素:
I want to draw every element of every subtuple with networkx declared via nodelist:
nx.draw_networkx_nodes(G, pos, nodelist=flattened_list_nodes, node_size=1600, node_color='blue', alpha=0.6)
为此,我将元组A展平为一个列表(每个元素仅出现一次):
For that i flattened the tuple A to a list (with every element only occuring once):
flattened_list_nodes = ['16.37.97.17', '178.237.19.228', '55177', '443', '6', '1', '46', '178.237.17.97', '44492', '16.37.97.29', '178.237.17.61', '56326', '16.37.97.222', '104.131.44.62', '60179', '80', '2', '620', '16.37.93.196', '16.37.157.74', '2049', '691', '100']
尽管我运行它,但出现错误:
Although when i run it, i get the error:
raise nx.NetworkXError('Node %s has no position.' % e)
networkx.exception.NetworkXError: Node '16.37.97.17' has no position.
我该如何解决?
我的完整代码:
for x in xrange(5):
G.add_edge('sIP:\n'+mostfrequent[x][0], countermfi[x])
G.add_edge('dIP:\n'+mostfrequent[x][1], countermfi[x])
G.add_edge('sPort:\n'+mostfrequent[x][2], countermfi[x])
G.add_edge('dPort:\n'+mostfrequent[x][3], countermfi[x])
G.add_edge('Protocol:\n'+mostfrequent[x][4], countermfi[x])
G.add_edge('Packets:\n'+mostfrequent[x][5], countermfi[x])
G.add_edge('Bytes:\n'+mostfrequent[x][6], countermfi[x])
pos = nx.kamada_kawai_layout(G) # positions for all nodes
#Hyperedges
nx.draw_networkx_nodes(G, pos, nodelist=countermfi, node_size=node_size, node_color='red', node_shape='s', alpha=1)
#Nodes
nx.draw_networkx_nodes(G, pos, nodelist=flattened_list_nodes, node_size=1600, node_color='blue', alpha=0.6)
#Edges
nx.draw_networkx_edges(G, pos, edgelist=G.edges(), width=2)
#Labels
nx.draw_networkx_labels(G, pos, font_size=11, font_family='sans-serif')
plt.axis('off')
plt.show()
预先感谢您, 问候:)
Thank you in advance, Greetings :)
推荐答案
IIUC:
首先创建图形G:
G = nx.Graph()
G.add_nodes_from(flattened_list_nodes)
pos = nx.kamada_kawai_layout(G)
nx.draw_networkx_nodes(G, pos, nodelist=flattened_list_nodes, node_size=1600, node_color='blue', alpha=0.6)
输出:
这篇关于Networkx-使用节点列表绘制节点时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文