0

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.

rob
  • 1
  • 2
  • Would it be possible to add a fake my_graph so that the code is runnable? It *looks* like it should work as you have it. – James A. Bednar Aug 11 '18 at 23:51
  • @JamesA.Bednar sure, I added a simple my_graph to my code. – rob Aug 12 '18 at 00:53
  • I was able to reproduce your problem, but I first added some nodes that are different from others to make it clearer: `my_graph.nodes[1]['height']='Short' ; my_graph.nodes[2]['degree']='PhD' ; my_graph.nodes[3]['preferredfood']='Pasta'` – James A. Bednar Aug 12 '18 at 12:42
  • For the HoloMap version, I only saw node coloring for `label="preferredfood"`. I also tried a DynamicMap equivalent, with similar behavior except coloring only for `"height"`: `hv.DynamicMap(get_graph, kdims=['label']).redim.values(label=["height","degree","preferredfood"])` – James A. Bednar Aug 12 '18 at 12:46
  • @JamesA.Bednar thanks for reproducing! So for now I'll just create a plot for each of the labels selected individually, I guess. – rob Aug 12 '18 at 15:01

0 Answers0