gpt4 book ai didi

python - 在 Bokeh、Python 中单击时为网络图节点线着色

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:11 24 4
gpt4 key购买 nike

通过将以下代码与 Bokeh 服务器结合使用,我目前可以通过从下拉列表中选择网络图中的选定节点来将其着色为粉红色。

我想做的是扩展代码,允许我在单击节点时执行相同的突出显示回调,使用Taptool()或其他一些方法。这可能吗?

import networkx as nx
from bokeh.models.graphs import from_networkx
from bokeh.models import Range1d, MultiLine, Circle, TapTool, Plot
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.layouts import row
from bokeh.models.widgets import Dropdown


def choose_node_outline_colors(node_clicked):
outline_colors = []
for node in G.nodes():
if str(node) == node_clicked:
outline_colors.append('pink')
else:
outline_colors.append('black')
return outline_colors

def update_node_highlight(attrname, old, new):
node_clicked = dropdown.value
data['line_color'] = choose_node_outline_colors(node_clicked)

G = nx.karate_club_graph()

plot = Plot(plot_width=400, plot_height=400,
x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))
graph = from_networkx(
G,
nx.circular_layout,
scale=1,
center=(0,0)
)

# Create nodes and edges
data = graph.node_renderer.data_source.data
data['line_color'] = choose_node_outline_colors('1')
graph.node_renderer.glyph = Circle(size=10, line_color="line_color")
graph.edge_renderer.glyph = MultiLine(line_alpha=1.6, line_width=0.5)

# Add tap tool
plot.add_tools(TapTool())

plot.renderers.append(graph)

# Dropdown menu to highlight a particular node
dropdown = Dropdown(label="Highlight Node", menu=list(map(str, list(G.nodes()))))
dropdown.on_change('value', update_node_highlight)
dropdown.value = '1'

curdoc().add_root(row(plot, dropdown))

enter image description here

最佳答案

好的,我设法使用 TapTool 并利用节点索引找到了解决方案:

import networkx as nx
from bokeh.models.graphs import from_networkx
from bokeh.models import Range1d, MultiLine, Circle, TapTool, Plot, HoverTool, BoxSelectTool
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.layouts import row
from bokeh.models.widgets import Dropdown
from bokeh.events import Tap


def choose_node_outline_colors(nodes_clicked):
outline_colors = []
for node in G.nodes():
if str(node) in nodes_clicked:
outline_colors.append('pink')
else:
outline_colors.append('black')
return outline_colors


def update_node_highlight(event):
nodes_clicked_ints = source.selected.indices
nodes_clicked = list(map(str, nodes_clicked_ints))
source.data['line_color'] = choose_node_outline_colors(nodes_clicked)


G = nx.karate_club_graph()

plot = Plot(plot_width=400, plot_height=400,
x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))
graph = from_networkx(
G,
nx.circular_layout,
scale=1,
center=(0,0)
)

# Create nodes and edges
source = graph.node_renderer.data_source
source.data['line_color'] = choose_node_outline_colors('1')
graph.node_renderer.glyph = Circle(size=10, line_color="line_color")
graph.edge_renderer.glyph = MultiLine(line_alpha=1.6, line_width=0.5)

# Add tap tool
TOOLTIPS = [
("Index", "@index"),
]
plot.add_tools(HoverTool(tooltips=TOOLTIPS), TapTool(), BoxSelectTool())

plot.renderers.append(graph)

taptool = plot.select(type=TapTool)

plot.on_event(Tap, update_node_highlight)

curdoc().add_root(plot)

关于python - 在 Bokeh、Python 中单击时为网络图节点线着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58430994/

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