1

I managed to solve half of my issue so I modified this post, the only problem I encounter now is a error type that has no record to find a solution, if anyone know why this error happens and can give me some advice or some links lead my way to a solution, it will be appreciate.

My nodes are list of lists from Dataframe, my edges are from a list of tuples.

I know below error may due to my nodes and edges involved in G=nx.Graph(), but they run well if I just need a static plot, but no idea how to modify it when I need a dynamic graph.

Below is part of my code:


    ...
    nodelist = [A, B, C, D, E, F, G, H, N, O, P, Q, R]
    #create empty graph
    G = nx.Graph()
    
    node_list = []
    for sublist in nodelist:
        for item in sublist:
            node_list.append(item)
    for i in node_list:
        G.add_node(i)  
    
    G.nodes()
    
    edges = set(AB+BC+CN+NE+ED+DO+OP+PQ+QH+HG+GF+FR+BE+HR)
    # edges
    
    G.add_edges_from(edges)
    
    pos = nx.fruchterman_reingold_layout(G, scale=4)
    
    #nx.draw_networkx(G, pos=pos, nodelist=nodelist[0], with_labels=False, node_size=10, node_color='#FF0000', font_size=5, width=2)
    ...
    plot = Plot(plot_width=400, plot_height=400,x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
        
    title = "track-dynamic graph"
        
    HOVER_TOOLTIPS = [("a", "@index"), ("b", "@type")]
        
    plot = figure(tooltips = HOVER_TOOLTIPS, tools="pan,wheel_zoom,save,reset", active_scroll='wheel_zoom',x_range=Range1d(-10.1, 10.1), y_range=Range1d(-10.1, 10.1), title=title)
        
    network_graph = from_networkx(G, nx.spring_layout, scale=10, center=(0, 0))
    
    network_graph.node_renderer.glyph = Circle(size=3, fill_color='blue')
        
    network_graph.edge_renderer.glyph = MultiLine(line_alpha=0.5, line_width=1)
        
    plot.renderers.append(network_graph)
        
    show(plot)

Above bokeh code gives me below error:

ValueError: failed to validate StaticLayoutProvider(id='1058', ...).graph_layout: expected an element of Dict(Either(String, Int), Seq(Any)), got {'6600d5067598ac904ac8e1ed3d3760c0': array([-1.67995763, 3.53275585]), 'd151da7d698cd5a6f6957ca77549ebbe': array([-5.69608212, 0.91764808])...

Alicia
  • 75
  • 8
  • Seems like the versions of Bokeh and networkx you have installed are incompatible. What are they? (You should always state relevant version information on every single question.) – bigreddot Apr 29 '21 at 03:50
  • The Bokeh version did not make it into your comment, and as I mentioned the networkx version is important and relevant as well. – bigreddot Apr 29 '21 at 05:44
  • Thanks for your reply, bokeh: 2.3.1, networkx : 2.5.1, sorry ya, not so familiar with the reply fuction so didn't finish my typing previously – Alicia Apr 29 '21 at 05:56

2 Answers2

0

You are calling from_networkx without suplying all the required parameters. You must supply the layout_function parameter, which controls what kind of layout you want. You can pass any of the layout functions built in to networkx. Supplying this required paramater is demonstrated explicitly in the Bokeh user's guide for networkx integration, e.g.

import networkx as nx

graph = from_networkx(G, nx.spring_layout, ...) # use spring_layout
bigreddot
  • 33,642
  • 5
  • 69
  • 122
  • 1
    Thank you so much for your reply, @bigreddot, I added the layout_function, but got a new kind of error.....ValueError: failed to validate StaticLayoutProvider(id='1058', ...).graph_layout: expected an element of Dict(Either(String, Int), Seq(Any)), got {'6600d5067598ac904ac8e1ed3d3760c0': array([-1.67995763, 3.53275585]), 'd151da7d698cd5a6f6957ca77549ebbe': array([-5.69608212, 0.91764808]), ....) – Alicia Apr 30 '21 at 00:42
  • I can't speculate further without a complete [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) to actually run and investigate. – bigreddot Apr 30 '21 at 04:01
  • @Alicia, I got the same error! How did you solve it? – errenmike1806 Nov 01 '22 at 09:15
  • @errenmike1806 please help others help you, by opening a question with a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – bigreddot Nov 02 '22 at 18:59
0

This's post to reproduce problem

Had the same problem on python 3.9.14 and bokeh 3.1.0

ValueError: failed to validate StaticLayoutProvider(id='p1076', ...).graph_layout: expected an element of Dict(Int, Seq(Any)), got {'Aemon': array([-0.31649242,  2.58870237]), 'Grenn': array([-0.74669326,  4.67785828]), 'Samwell': array([-2.08387338,  4.10110813]), 'Doran': array([4.01849557, 1.36418261]), 'Walton': array([-2.55845497, -4.56622778])}

To reproduce problem:

  1. download jupyter notebook from https://github.com/melaniewalsh/Intro-Cultural-Analytics/blob/master/book/06-Network-Analysis/02-Making-Network-Viz-with-Bokeh.ipynb
  2. download data from https://github.com/melaniewalsh/Intro-Cultural-Analytics/blob/master/book/data/got-edges.csv
  3. run notebook

p.s. haven't enough reputation to post it as comment to @bigreddot message.

  • https://discourse.bokeh.org/t/calling-from-networkx-using-a-graph-with-string-nodes/9719 - "Bokeh graph renderers currently expect integer node indices to be used." – Mutiev Fedor Apr 16 '23 at 06:23