2011-10-17 46 views
6

我有一个数据集,我将其上传为各种时间表的图表,并试图找出它们之间的关系。如何删除networkx中的节点?

我想删除所有没有边的节点,但我不确定删除或删除节点的命令。任何想法如何做到这一点?

回答

12
import networkx as nx 
import matplotlib.pyplot as plt 

G=nx.Graph() 
G.add_edges_from([('A','B'),('A','C'),('B','D'),('C','D')]) 
nx.draw(G) 
plt.show() 

enter image description here

G.remove_node('B') 
nx.draw(G) 
plt.show() 

enter image description here

要删除多个节点,还存在的Graph.remove_nodes_from()方法。

3

Documentation覆盖它。

Graph.remove_node(n):删除节点n。

Graph.remove_nodes_from(nodes):删除多个节点。

例如:

In : G=networkx.Graph() 

In : G.add_nodes_from([1,2,3]) 

In : G.nodes() 
Out: [1, 2, 3] 

In : G.remove_node(2) 

In : G.nodes() 
Out: [1, 3]