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

如何调整Python中Netgraph库的节点边框宽度?

调整Netgraph节点边框宽度的方法

方法一:绘图时直接指定参数

Netgraph的绘图函数(draw() 或 interactive_draw())支持 node_edge_width 参数,传入具体数值即可控制节点边框粗细,数值越大边框越宽:

import matplotlib.pyplot as plt
from netgraph import draw

# 示例数据
nodes = [1, 2, 3]
edges = [(1, 2), (2, 3)]

# 设置节点边框宽度为2(默认值通常为1)
draw(nodes, edges, node_edge_width=2)
plt.show()

方法二:动态调整已绘制的节点

如果图形已绘制完成,可通过获取节点对应的matplotlib艺术家对象修改边框宽度:

import matplotlib.pyplot as plt
from netgraph import draw

nodes = [1, 2, 3]
edges = [(1, 2), (2, 3)]

# 绘图并返回包含所有元素艺术家的字典
artist_collection = draw(nodes, edges)

# 遍历所有节点艺术家,设置边框宽度为3
for node_artist in artist_collection['nodes'].values():
    node_artist.set_linewidth(3)

plt.show()

针对InteractiveGraph的情况

若使用交互式图形类 InteractiveGraph,可在初始化时传入 edge_width 参数,或后续调用 set_node_properties() 方法调整:

from netgraph import InteractiveGraph
import matplotlib.pyplot as plt

nodes = [1, 2, 3]
edges = [(1, 2), (2, 3)]

# 初始化时设置节点边框宽度
graph = InteractiveGraph(nodes, edges, node_edge_width=2)

# 后续动态调整单个或所有节点边框
graph.set_node_properties(node=1, edge_width=3)  # 调整节点1的边框
plt.show()

内容的提问来源于stack exchange,提问作者Franck

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 00:52:06