Alan Valejo
Alan Valejo

Reputation: 1315

How to draw graphs with vertices and edges of different sizes using the holoviews library?

How to draw a graph with vertices and edges of different sizes? I'm using ipython and holoviews library.

For instance,

Edges input

start,ends
1,4
2,4
3,5
3,6
2,6

Nodes input

x,y,index,type
0.0,1.0,1,a
0.5,1.0,2,a
1.0,1.0,3,a
0.4,0.0,4,b
0.5,0.0,5,b
0.6,0.0,6,b

main.py

import numpy as np
import pandas as pd
import holoviews as hv
import networkx as nx
from holoviews.operation.datashader import datashade, bundle_graph

hv.extension('bokeh')

%opts Nodes Graph [width=800 height=200 xaxis=None yaxis=None]
%opts Graph (node_size=8 edge_line_width=0.5)

colors = ['#000000']+hv.Cycle('Category20').values

edges_df = pd.read_csv('edges.csv')
nodes = hv.Nodes(pd.read_csv('nodes.csv')).sort()

graph = hv.Graph((edges_df, nodes))
graph = graph.redim.range(x=(-0.05, 1.05), y=(-0.05, 1.05)).opts(style=dict(cmap=colors))

bundled = bundle_graph(graph)
bundled

Output

enter image description here

I tried to modify this lines:

node_size=[3,4,5,8,3,8]
edge_size=[0.5,0.6,0.8,1.5,0.5]
%opts Graph (node_size=node_size edge_line_width=edge_size)

However, this is not working.

Can anyone help me? How to create nodes and edges of different sizes?

Upvotes: 0

Views: 1486

Answers (2)

philippjfr
philippjfr

Reputation: 4080

Currently scaling of node sizes and edge line width is not exposed directly (although it soon will be), however you can directly modify the data source and glyph to do this kind of thing. Here is an example defining a so called finalize_hook which will modify the data source and glyph to let you define custom scaling of both:

node_size=[3,4,5,8,3,8]
edge_size=[0.5,0.6,0.8,1.5,0.5]

def scale_sizes(plot, element):
    plot.handles['scatter_1_source'].data['node_size'] = node_size
    plot.handles['scatter_1_glyph'].size = 'node_size'

    plot.handles['multi_line_1_source'].data['edge_size'] = edge_size
    plot.handles['multi_line_1_glyph'].line_width = 'edge_size'

%opts Graph [finalize_hooks=[scale_sizes]]

Upvotes: 2

Alan Valejo
Alan Valejo

Reputation: 1315

I solved the node size problem with a new column in the input file.

x,y,index,type,size
0.0,1.0,1,a,8
0.5,1.0,2,a,10
1.0,1.0,3,a,20
0.4,0.0,4,b,8
0.5,0.0,5,b,3
0.6,0.0,6,b,15

After, I seted the column name in options of the graph

%opts Graph (node_size='size' edge_line_width=0.5)

However, this strategy does not work for edge size.

enter image description here

Upvotes: 1

Related Questions