Setting node size in `pyvis`

243 views Asked by At

I use pyvis to visualize graphs. Everything works well, except for the fact that the node labels are super small, and I need to perform extreme zoom-in in order to see them (see the screenshot)

Is there a way to make the node labels much larger?

Here's a minimal example:

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

And here's the screenshot: a screnshot

1

There are 1 answers

0
ndclt On

Here how I increase the size of the label:

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")

I follow the tricks given in the pyvis documentation by showing the all interactive button with the g.show_buttons() function, choosing the one that fits the needs and generate the options.

If you set some option by code (like the toggle_physics one), put the call to set_options after otherwise it will fail with the following error:

    self.options.physics.enabled = status
    ^^^^^^^^^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'physics'