gpt4 book ai didi

python - 将节点标签添加到 Bokeh 网络图

转载 作者:太空狗 更新时间:2023-10-29 18:03:31 27 4
gpt4 key购买 nike

我正在使用以下代码生成交互式 Bokeh 网络图。如何将节点名称添加到 Bokeh 图中的节点?

from bokeh.io import show, output_notebook
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
from bokeh.models import LabelSet

plot = Plot(plot_width=900, plot_height=500,
x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))
plot.title.text = "Graph Interaction Demonstration"

plot.add_tools(HoverTool(tooltips=None), TapTool(), BoxSelectTool())

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

graph_renderer.node_renderer.glyph = Circle(size=15, 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.node_renderer.glyph.properties_with_values()
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 = EdgesAndLinkedNodes()

plot.renderers.append(graph_renderer)

show(plot)

生成的 Bokeh 网络图: enter image description here

最佳答案

我猜你的意思是节点的交互式标签,我也想做的事情。为此,您需要修改几行:

hover = HoverTool(tooltips=[("Name:", "@name")])
plot.add_tools(hover, TapTool(), BoxSelectTool(), WheelZoomTool())
...
graph_renderer.inspection_policy = NodesAndLinkedEdges()

然后修改节点的数据源:

graph_renderer.node_renderer.data_source.data['name'] = [name1, ... ,nameN]

(数据源已经存在;它是一个字典,唯一的键是“索引”,一个编号的节点列表。因此您可以添加更多引用相同长度列表的键 - 例如名称列表 -这些列表可以通过“@key”访问)

关于python - 将节点标签添加到 Bokeh 网络图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47210530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com