Oval shapes for holoviews.Nodes

Hi everyone,

I’m writing a Jupyter notebook that takes a Netwokx graph with multiple data over the nodes and tries to draw it with Holoviews in order to best show the connections between nodes.
For this purpose, I’ve found easier to split the Networkx structure and populate hv.Nodes, hv.Graph and hv.Labels. I would love to be able to set the nodes shape to ovals instead of circles or rectangles since some labels are pretty long. Is there a way to do this? Looking at the documentation I could not find a oval shape, but just triangles, rectangles, crosses and circles.

Thanks for any help!

Looks like to do this we need to use the hooks option.

Adapting from the holoviews example:

import numpy as np
import holoviews as hv
from holoviews import opts
import networkx as nx
from bokeh.models import GraphRenderer, Ellipse

opts.defaults(opts.Graph(width=400, height=400))

# Declare abstract edges
N = 8
node_indices = np.arange(N)
source = np.zeros(N)
target = node_indices

G = nx.DiGraph()

for t in target:
    G.add_edge(0, t)

#for inspecting / tinkering with handler list; was only used for troubleshooting in later Jupyter cells
glyph_handlers = []

def hook(plot,element):
    glyphs = [r for r in plot.handles.items() if 'glyph' in r[0]]
    glyph_handlers.extend(glyphs) # this was just used for troubleshooting if you want to be able to inspect them later to target different parts of the plot with your hook code
    target_renderer = glyphs[1][1] #this indexing was chosen after inspecting the handler order; probably need a more reliable way for plots with more layers
    target_renderer.glyph = Ellipse(height=0.1, width=0.2,fill_color="fill_color")

simple_graph = hv.Graph.from_networkx(G,nx.layout.circular_layout).opts(hooks=[hook])


1 Like