单击Bokeh中的彩色网络图节点线,Python [英] Color network graph node lines on click in Bokeh, Python

查看:61
本文介绍了单击Bokeh中的彩色网络图节点线,Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Bokeh服务器上使用以下代码,我现在可以通过从下拉列表中选择它来将网络图中的选定节点涂成粉红色.

Using the following code with Bokeh server, I am currently able to color a selected node in my network graph pink by selecting it from a dropdown.

我想做的是扩展代码来做允许我点击 Taptool()来执行相同的突出显示回调.或其他方法.这可能吗?

What I'd like to do is extend the code to do is allow me to perform the same highlight callback, when a node is clicked, using Taptool() or some other method. Is this possible?

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))

推荐答案

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

Ok, I managed to find a solution using TapTool and making use of the node index:

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)

这篇关于单击Bokeh中的彩色网络图节点线,Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆