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

如何调大pyvis可视化图中的节点标签尺寸?

解决Pyvis节点标签过小的问题

方法1:为单个节点指定字体大小

在给NetworkX节点添加标签时,同时添加font属性并设置size值,让每个节点的标签使用指定大小:

import networkx as nx
from pyvis.network import Network

GG = nx.gnp_random_graph(100, 0.02, directed=True)
for n in GG.nodes:
    GG.nodes[n]["label"] = f"Node {n:03d}"
    # 设置字体大小为20,可根据需求调整数值
    GG.nodes[n]["font"] = {"size": 20}

g = Network(cdn_resources="in_line", notebook=True)
g.toggle_physics(True)
g.toggle_hide_edges_on_drag(True)
g.force_atlas_2based()
g.from_nx(GG)
g.show("graph.html")
!open graph.html

方法2:全局统一调整所有节点字体大小

如果需要批量设置所有节点的标签大小,可通过set_options方法用vis.js的配置语法全局配置,无需逐个修改节点:

import networkx as nx
from pyvis.network import Network

GG = nx.gnp_random_graph(100, 0.02, directed=True)
for n in GG.nodes:
    GG.nodes[n]["label"] = f"Node {n:03d}"

g = Network(cdn_resources="in_line", notebook=True)
g.toggle_physics(True)
g.toggle_hide_edges_on_drag(True)
g.force_atlas_2based()
g.from_nx(GG)

# 全局设置节点字体大小,此处size设为20,可按需调整
g.set_options("""
{
  "nodes": {
    "font": {
      "size": 20
    }
  }
}
""")

g.show("graph.html")
!open graph.html

补充提示

  • 两种方法可结合使用:先全局设置基础字体大小,再为特定节点单独配置不同大小。
  • 字体大小数值可灵活调整(如16、24等),直到标签清晰易读为止。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 20:51:12