简体   繁体   中英

(Python) Networkx - How to set own positions for nodes with pos variable

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

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM