0

my code is like that:

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

My output of print G.nodes(data=True) is:

[('Bytes:\n620', {}), ('dIP:\n178.237.19.228', {}), ('sPort:\n2049', {}), ('sPort:\n60179', {}), ('sIP:\n16.37.97.29', {}), (153, {}), ('dPort:\n443', {}), ('dPort:\n80', {}), ('Packets:\n2', {}), ('Packets:\n1', {}), ('sPort:\n44492', {}), ('Bytes:\n100', {}), ('sIP:\n16.37.93.196', {}), ('dIP:\n178.237.17.97', {}), (188, {}), ('dIP:\n16.37.157.74', {}), ('sIP:\n16.37.97.222', {}), ('dIP:\n178.237.17.61', {}), ('sIP:\n16.37.97.17', {}), ('Bytes:\n46', {}), (224, {}), (227, {}), ('dPort:\n691', {}), ('dIP:\n104.131.44.62', {}), ('sPort:\n55177', {}), ('Protocol:\n6', {}), (120, {}), ('sPort:\n56326', {})]

I have a problem with using nx.kamada_kawai_layout, because i get an error:

raise nx.NetworkXError('Node %s has no position.' % e)
networkx.exception.NetworkXError: Node '16.37.97.17' has no position.

How can i fix this or set my own pos for every of the 40 nodes?

Thank you in advance, Greetings :)

QWERASDFYXCV
  • 245
  • 1
  • 6
  • 15

1 Answers1

0

Based on your other question, your problem is that the node '16.37.97.17' that appears in flattened_list_nodes is not a node in the graph. When you added the edges, you appended the node name with some text. So you added the node 'sIP:\n16.37.97.17'

Then you ask networkx to plot the node '16.37.97.17', but that isn't part of the graph and because of that, pos doesn't have a position for it. So it's going to fail. You need to either not change the name of the node before adding it to the graph, or use the changed name in your nodelist.

Even if you define a position for that node, it will still fail to plot because the node isn't in the graph.

Joel
  • 22,598
  • 6
  • 69
  • 93
  • Ok, i understand, but how do i fix it to still have the labels in front the data? Can i add the label to the flattened_list_nodes with a for loop? Or what is the best solution? And how do i set my own pos dictionary for the nodes then? – QWERASDFYXCV Oct 28 '18 at 13:56
  • Instead of adding extra text to the node name, look at the last part of https://stackoverflow.com/a/28533293/2966723. If you really want to add text to the node names, then you need to update your nodelists to include that extra text. Otherwise in the drawing command where you ask it to plot `'16.37.97.17'` it has no way of knowing that it's really supposed to be plotting `'sIP:\n16.37.97.17'`. But your best option is to update the node labels rather than the node name as in the answer I linked to. – Joel Oct 29 '18 at 20:49