在NetworkX中绘制隔离的节点

时间:2019-06-25 20:40:36

标签: python networkx graph-theory

import networkx as nx
import numpy as np
from scipy.sparse import coo_matrix #coordinate sparse matrices

A = np.zeros([4,4])
A[0,1] = A[1,2] = 1
S = coo_matrix(A)
edges = np.r_[[S.row], [S.col]].T
G = nx.Graph()
G.add_edges_from(edges)
nx.draw(G)

运行该脚本时,我得到了:

3-node network

但是有四个节点。如何显示孤立的第四个节点?

1 个答案:

答案 0 :(得分:2)

仅通过将边添加到图上,networkx无法知道其他顶点。它所做的就是添加要提供的每个边的顶点。相反,如果您显式添加所有顶点,那么就可以了:

G = nx.Graph()
G.add_nodes_from(range(len(A)))
G.add_edges_from(edges)
nx.draw(G)

enter image description here