holoviz / holoviews

With Holoviews, your data visualizes itself.
https://holoviews.org
BSD 3-Clause "New" or "Revised" License
2.7k stars 403 forks source link

hv.Graph.from_networkx(): node coloring fails if graph has edge attributes #5707

Open MikeB2019x opened 1 year ago

MikeB2019x commented 1 year ago

macOS: 13.3.1 (22E261) python: 3.10.10 Jupyter: 6.5.4 (server) holoviews: 1.15.4

The method should take in a graph and give a visualization. The nodes in the visualization can be color-coded according to any of the node attributes. Note the documentation indicates that "...By default it will extract all node and edge attributes ..."

If as in the example below, a graph is constructed and both the nodes and edges have attributes. The result is a graph visualization where the nodes have no color if a color is linked to a node attribute.

g = nx.Graph()
g.add_node('n10747', **{'TYPE': ':InvoiceLineItem','label': '10'})
g.add_node('n325', **{'TYPE': ':Invoice', 'label': '20'})
g.add_edge('n325', 'n10747', **{'label': 'HAS', 'TYPE': 'HAS', 'id': 'e74229'})

g_prime = g.subgraph(['n325','n10747'])
position_g = nx.layout.spring_layout(g_prime)
g_plot = hv.Graph.from_networkx(g_prime, position_g)
g_plot.opts(node_color='TYPE', cmap='Category10')

image

If the same graph is constructed but without any edge attributes, the nodes are colored as expected.

g = nx.Graph()
g.add_node('n10747', **{'TYPE': ':InvoiceLineItem','label': '10'})
g.add_node('n325', **{'TYPE': ':Invoice', 'label': '20'})
g.add_edge('n325', 'n10747')

g_prime = g.subgraph(['n325','n10747'])
position_g = nx.layout.spring_layout(g_prime)
g_plot = hv.Graph.from_networkx(g_prime, position_g)
g_plot.opts(node_color='TYPE', cmap='Category10')

image

There is no error thrown.
For those wondering: same behaviour if nodes/edges are entered in key word fashion e.g. g.add_edge('n325', 'n10747', label='HAS', TYPE='HAS', id='e74229')

MikeB2019x commented 1 year ago

The situation has deteriorated further? Running the same code as my preceding post:

holoviews: 1.16.0
bokeh: 2.4.3

image

hoxbro commented 1 year ago

This is a bug in Bokeh with WebGL and should be fixed in the next release. Until then, you can get the desired behavior with hv.renderer("bokeh").webgl = False.

MikeB2019x commented 1 year ago

I just have to say, when the graph/network rendering works in holoviews/bokeh it is awesome. Like this (I'm not showing the hovering)

image

It would be really perfect if the holoviews team and the networkx team could maybe collaborate/advise on bringing awesome graphics to the networkx package. The latter is a great package but if the visualization (see below) was as good as holoviews/bokeh it would be magnificent.

image

yeah, I'm a big fan of both these products and not a member of either team :-D

MikeB2019x commented 1 year ago

This is a bug in Bokeh with WebGL and should be fixed in the next release. Until then, you can get the desired behavior with hv.renderer("bokeh").webgl = False.

While this allows rendering the node markers, this doesn't change that the colouring of the nodes doesn't happen unless the edge attributes are cleared.