简体   繁体   中英

Unable to show labels from NetworkX using HoverTool in Bokeh

I have been trying to create an interactive graph over respondents and answer categories for an interview study with Bokeh, but I am unable to get the labels to appear when hovering over the nodes. I have tried using solutions from Adding node labels to bokeh network plots but I keep running into the same issues as Kristada673 describes in the comments.

Any ideas?

from bokeh.io import show, output_file
from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, TapTool, BoxSelectTool
from bokeh.models.graphs import from_networkx, NodesAndLinkedEdges, EdgesAndLinkedNodes
from bokeh.palettes import Spectral4
G=nx.Graph()

respondent = ["TI01", "TI02", "TI03", "TT01", "TT02", "TT03"]  #Respondents

#             0                        1                       2                   3           4                             5              6                     7              8              9                10                11                    12             13                 14
code=["Previous Teaching", "Avoidance Strategies", "Spelling by Sound", "Homophones", "Individual Oral Feedback", "Written Feedback", "Articles", "Overwhelmed Students", "Verb Tenses", "Punctuation", "Formal Register", "Run-on Sentences", "Matrices/Rubrics", "Peer-review", "Sentence Structures"]  #Substantive Codes

G.add_nodes_from(respondent)

G.add_nodes_from(code)

#First Pilot
#TI01
G.add_edges_from([("TI01", code[0]), ("TI01", code[2]), ("TI01", code[5])])
#TI02
G.add_edges_from([("TI02", code[2]), ("TI02", code[3]), ("TI02", code[0]), ("TI02", code[4]), ("TI02", code[5])])
#TI03
G.add_edges_from([("TI03", code[0]), ("TI03", code[1]),("TI03", code[5])])

#Second Pilot
#TT01
G.add_edges_from([("TT01", code[2]), ("TT01", code[3]), ("TT01", code[4]), ("TT01", code[5])])
#TT02
G.add_edges_from([("TT02", code[6]), ("TT02", code[9]), ("TT02", code[8]), ("TT02", code[10]), ("TT02", code[0]), ("TT02", code[13])])
#TT03
G.add_edges_from([("TT03", code[14]), ("TT03", code[0]),("TT03", code[13]), ("TT03", code[12]), ("TT03", code[9]), ("TT03", code[11])])


plot = Plot(plot_width=600, plot_height=600,
            x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))

plot.title.text = "Possible Substantive Codes Pilot Interviews"

hover = HoverTool(tooltips=[('respondent','code')])
plot.add_tools(hover, TapTool())

graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0,0))

graph_renderer.node_renderer.glyph = Circle(size=20, fill_color=Spectral4[0])
graph_renderer.node_renderer.selection_glyph = Circle(size=15, fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = Circle(size=15, fill_color=Spectral4[1])


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

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


plot.renderers.append(graph_renderer)

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

This got solved by referring to "@index" in the HoverTool and using the output file rather than looking at the new browser window. The labels still do not work in the new browser window but do work when opening the output file in the browser.

If you are running into the same issue, try using the file generated by the code to see if it is fixed in there.

Not a complete solution, but solved for my purposes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM