在`pyvis`中设置节点大小

问题描述 投票:0回答:1

我使用 pyvis 来可视化图表。一切都运行良好,除了节点标签非常小,我需要执行极端放大才能看到它们(参见屏幕截图)

有没有办法让节点标签变得更大?

这是一个最小的例子:

import networkx as nx

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)
g.show("graph.html")
!open graph.html

这是屏幕截图:

python visualization pyvis
1个回答
0
投票

这是我如何增加标签尺寸的:

from pyvis import network
import networkx as nx

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.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.set_options("""
var options = {
  "nodes": {
    "borderWidth": null,
    "borderWidthSelected": null,
    "opacity": null,
    "font": {
      "size": 52,
      "face": "verdana"
    },
    "size": null
  }
}
""")
g.show("graph.html")

我遵循 pyvis 文档中给出的技巧,通过使用

g.show_buttons()
功能显示所有交互式按钮,选择适合需求的按钮并生成选项。

如果您通过代码设置某些选项(例如

toggle_physics
),请在之后调用
set_options
,否则它将失败并出现以下错误:

    self.options.physics.enabled = status
    ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'physics'
© www.soinside.com 2019 - 2024. All rights reserved.