1

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', {})]

How can i set the position (coordinates) of the nodes manually by myself with the pos variable?

Thank you in advance,

Greetings :)

QWERASDFYXCV
  • 245
  • 1
  • 6
  • 15
  • Based on your other questions https://stackoverflow.com/q/52987548/2966723 & https://stackoverflow.com/q/52971894/2966723, this is not the problem your code is running into. You've defined a graph, and you've created `pos`. Then you try to draw the graph with a `nodelist` that includes nodes which are not in the graph because you changed the name of the nodes when you added them to the graph. All the nodes in the graph already have coordinates [check it - `print(pos)`]. The problem is that your nodelist is using a different name that networkx cannot recognize. – Joel Oct 30 '18 at 01:54
  • Or to present it another way. Run the code `for node in nodelist: if node not in G.nodes: print('node {} will produce an error because it isn't a node of G.'.format(node))` – Joel Oct 30 '18 at 01:56

1 Answers1

1

I don't think the question you have asked will actually solve the problem you are running into, but it's still a reasonably common issue so it's worth explaining.

pos is simply a dictionary whose keys are the nodes of the graph and whose values are the 2-d positions of the nodes.

Here's how you do it.

import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edge(0,1)
G.add_node(2)
pos = {}
pos[0] = (0,0)
pos[1] = (1,0)
pos[2] = (0.5, 1)
nx.draw_networkx(G, pos)
plt.show()

enter image description here

Joel
  • 22,598
  • 6
  • 69
  • 93