You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何NetworkX未显示顶点0与2间的边?代码问题咨询

问题原因与解决办法

首先你的代码里缺少import torch语句,先补上才能正常运行。

至于你看不到顶点0和2之间的边,本质是边其实存在,只是默认绘图参数导致它被节点遮挡或视觉上不明显。

验证边是否存在

在代码里添加一行打印边的代码,就能确认边已经被正确导入到NetworkX图中:

print(g.edges())

运行后会输出类似[(0, 1), (1, 2), (0, 2)]的结果,说明0和2之间的边确实存在。

调整绘图参数让边显示清晰

修改nx.draw的参数,通过减小节点尺寸、增大边宽、设置显眼的边颜色,就能清楚看到所有边:

import torch
import networkx as nx
from torch_geometric.data import Data

edge_index = torch.tensor([[0, 1], [1, 2], [2, 0]], dtype=torch.long)
x = torch.eye(3, dtype=torch.float)
data = Data(x=x, edge_index=edge_index.t().contiguous())
g = torch_geometric.utils.to_networkx(data, to_undirected=True)

# 带参数绘图,确保边可见
nx.draw(g, node_size=150, width=2.0, edge_color='darkred', with_labels=True)

如果用nx.draw_circular,同样加上这些参数也能看到完整的三角形边。

内容的提问来源于stack exchange,提问作者Mohammad Ali Nematollahi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 11:15:59