3

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

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)

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.

How can i fix this?

My complete code:

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

QWERASDFYXCV
  • 245
  • 1
  • 6
  • 15
  • How have you defined `pos`? It needs to be a dict, and it needs to assign a position to all of your nodes. networkx has commands that will create `pos` for you. – Joel Oct 24 '18 at 18:52
  • I have defined pos like this: `pos = nx.kamada_kawai_layout(G)`. If i use `nx.draw_networkx_nodes(G, pos, node_size=3000, node_color='deepskyblue', alpha=1)` to draw nodes it works, but i want to draw 2 different style of nodes with 2 arrays as nodelist. Setting the other array as nodelist works strangely enough. – QWERASDFYXCV Oct 24 '18 at 19:56
  • can you edit your question to provide more of the code? What you're describing suggests to me that the error is somewhere else. – Joel Oct 24 '18 at 22:33
  • Basically the error message is saying that `pos` does not have a value for `'16.37.97.17'` – Joel Oct 24 '18 at 22:44
  • Ok, updated my question to provide more of my code. Hope that helps to get a solution. Also it is possible to define my own 'pos' of the 40 nodes with a dictionary? If yes, how do i know which node has which number for defining their position? – QWERASDFYXCV Oct 24 '18 at 23:28
  • @Joel Yeah but why?? Shouldn't have every node a position when i use a layout like kamada_kawai_layout? – QWERASDFYXCV Oct 24 '18 at 23:33
  • Now that I see the code, we can narrow things down. it's clear that `G` doesn't change at all once `pos` is created. So that means that the problem isn't that the `'16.37.97.17'` was added to `G` after defining `pos`. It seems clear that the problem is that `'16.37.97.17'` isn't added to `G` ever, but one of the nodelists that is passed to `nx.draw_networkx_nodes` does contain it. To be sure of that, print `G.nodes`. Then the question is, where did you expect it to get added? – Joel Oct 25 '18 at 01:24
  • print G.nodes(data=True): `[('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', {})` – QWERASDFYXCV Oct 25 '18 at 10:43
  • `, ('sIP:\n16.37.97.17', {}), ('Bytes:\n46', {}), (224, {}), (227, {}), ('dPort:\n691', {}), ('dIP:\n104.131.44.62', {}), ('sPort:\n55177', {}), ('Protocol:\n6', {}), (120, {}), ('sPort:\n56326', {})]` Thats my output of print G.nodes(data=True). – QWERASDFYXCV Oct 25 '18 at 10:43
  • @Joel [link]https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.DiGraph.add_edge.html[link] "Add an edge between u and v. The nodes u and v will be automatically added if they are not already in the graph." So the node `'16.37.97.17'` should be added with G.add_edge, like all the other nodes who have been added this way and have a pos. – QWERASDFYXCV Oct 25 '18 at 10:47
  • I see `'sIP:\n16.37.97.17'` as a node of `G`, but not `'16.37.97.17'`. So that's where the error is. When you're adding your edges, you're modifying the nodes' names. So `'sIP:\n16.37.97.17'` gets a position, but you've asked it to plot `'16.37.97.17'` which doesn't have a position. – Joel Oct 25 '18 at 15:05
  • 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? – QWERASDFYXCV Oct 28 '18 at 13:55

1 Answers1

1

IIUC:

First create graph, 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)

Output:

enter image description here

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • That works technically, but my whole graph is messed up when adding nodes separately. I use `for x in xrange(5): G.add_edge(mostfrequent[x][0], countermfi[x]) G.add_edge(mostfrequent[x][1], countermfi[x])` – QWERASDFYXCV Oct 24 '18 at 22:24