Graph Construction

Hi,
I have started to use bokeh library, to visualize graph. It needs a edge list using NetworkX graph instance. But, everytime we plot the distribution of graphs is different.

I wanted to know about the algorithm which to used to generate the graph using
graph_render (an from_networkx instance) using Plot function from boke.models.

@amsi in general it’s not possible to say much without details/code. But IIRC the layout comes from NetworkX, in which case this is not really a Bokeh question.

I used this code to generate graph.

plot = Plot(plot_width=2000, plot_height=2000,
            x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
plot.title.text = "GRMViz"
node_indices = [i+1 for i in range(len(Gg.nodes()))]

node_hover_tool = HoverTool(tooltips=[("index", "@index"), ("typeid", "@typeid"),("content","@content")])

plot.add_tools(node_hover_tool, BoxZoomTool(), ResetTool())

graph_renderer = from_networkx(Gg, nx.spring_layout, scale=1, center=(0, 0))

graph_renderer.node_renderer.glyph = Circle(size=10, fill_color="color")
graph_renderer.node_renderer.hover_glyph = Circle(size=20, fill_color=Spectral4[2])

'''graph_renderer.node_renderer.data_source.data = dict(
    index=node_indices,
    fill_color=cl)'''

graph_renderer.edge_renderer.glyph = MultiLine(line_color="#8073ac", line_alpha=0.8, line_width=5)
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=10)
#graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=10)

graph_renderer.selection_policy = NodesAndLinkedEdges()
#graph_renderer.inspection_policy = EdgesAndLinkedNodes()

plot.renderers.append(graph_renderer)

output_file("grmviz.html")
show(plot)

Hi @amsi please edit your post to use code formatting. (either with the </> icon on the editing toolbar, or triple backtick ``` fences around the code blocks)

Hi @Bryan, thanks for the edit feedback.
Can you help me with the query.

Thnaks in advance.

@amsi as I mentioned earlier, I think this seem to be a networkx question, not a Bokeh one, This line:

graph_renderer = from_networkx(Gg, nx.spring_layout, scale=1, center=(0, 0))

configures networkx to use the “spring layout”. That is part of networkx, not part of Bokeh. AFAIK it will generate a new random spring layout every time it is used. There are other layouts provided by networkx:

https://networkx.github.io/documentation/stable/reference/drawing.html#layout

It’s possible some of those are more stable / will generate the same layout reliably. I am afraid I don’t know, you will have to try / investigate on your own. If not, or if you really want a spring layout, you would need to compute your layout once, I think you can just call nx,spring_layout yourself:

layout = nx.spring_layout(Gg)

And then you can pass this layout to from_networkx. But again, AFAIK nx.spring_layout will return a different layout every time it is called. So if you really want this to be the same in between different processs, you will have to find a way to persist the returned layout outside the process and re-load it. (Maybe a simple pickle woudl suffice, I don’t know) And of course: that can only possibly be expected work if the input graph is the same in every process.