ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python-Networkx-使用节点列表绘制节点时出现问题

2019-10-12 17:55:57  阅读:608  来源: 互联网

标签:nodelist python networkx nodes draw


我有一个这样的嵌套元组:

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

我想用通过nodelist声明的networkx来绘制每个子元组的每个元素:

nx.draw_networkx_nodes(G, pos, nodelist=flattened_list_nodes, node_size=1600, node_color='blue', alpha=0.6)

为此,我将元组A展平为一个列表(每个元素仅出现一次):

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']

虽然我运行它时出现错误:

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

先感谢您,
问候 :)

解决方法:

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)

输出:

enter image description here

标签:nodelist,python,networkx,nodes,draw
来源: https://codeday.me/bug/20191012/1902212.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有