I've created a graph using networkx in a jupyter notebook. Each node of the graph has three different attributes. Layout (spring_layout) and visualising the graph with holoviews, I want to create a widget where I can choose which attributes to be taken as 'color_index' so that I can quickly visualise the clustering of nodes with respect to the attributes. The edge weights are computed from an engineered feature which is not part of the attributes.
Think of a graph of people as nodes whose edge weights are shared interests and the three attributes of the nodes could be 'height', 'degree' and 'preferred food'.
I ran into the problem that holoviews accepts the 'color_index' only in 'plot options', so even in an interactive widget only one of the attributes actually gets used as 'color_index'.
Here's the relevant part of my code (I call the attributes 'label'):
import holoviews as hv
import networkx as nx
my_graph = nx.star_graph(5)
nx.set_node_attributes(my_graph, name="height", values="Tall")
nx.set_node_attributes(my_graph, name="degree", values="MSc")
nx.set_node_attributes(my_graph, name="preferredfood", values="Pizza")
def get_graph(label):
plot_opts = dict(color_index = label,
edge_color_index = "weight",
width = 500,
height = 500,
xaxis = None,
yaxis = None,
show_frame = False,
tools = ['hover'])
style_opts = dict(node_size = 15,
cmap = "RdYlBu",
node_alpha = 0.95,
edge_cmap = "blues",
edge_line_width = 0.85,
edge_alpha = 1)
return hv.Graph.from_networkx(my_graph,
layout_function = nx.spring_layout,
k = 15,
iterations = 10,
scale = 2,
random_state = 100).opts(style = style_opts,
plot = plot_opts)
hv.HoloMap({label: get_graph(label) for label in ["height","degree","preferredfood"]}, kdims=["label"])
Probably my approach is off, but I'm not sure how to proceed.
Edit: I mocked up a my_graph in the code above, this should run now.