简体   繁体   English

在NetworkX中绘制隔离的节点

[英]Plotting isolated nodes in NetworkX

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)

When I run that script, I get this: 当我运行该脚本时,得到以下信息:

3节点网络

But there are four nodes. 但是有四个节点。 How can I get the isolated fourth node to show? 如何显示孤立的第四个节点?

By only adding the edges to the graph, networkx has no way of knowing about the additional vertices; 通过仅将边线添加到图形,networkx无法知道其他顶点。 all it's doing is adding the vertices of each edge that you're providing. 它所做的就是添加要提供的每个边的顶点。 If, instead, you explicitly add all vertices, then you're good to go: 相反,如果您显式添加所有顶点,那么就可以了:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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