Issue
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
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?
Solution
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]]
Answered By - philippjfr
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.