繁体   English   中英

使用 Networkx 和 Bokeh 拖动节点

[英]Drag nodes using Networkx and Bokeh

我正在使用带有networkx的散景来可视化这样的图形:( https://docs.bokeh.org/en/latest/docs/user_guide/graph.html#node-and-edge-attributes

我想知道是否可以拖动节点。 我已经尝试过 PointDrawTool ( https://docs.bokeh.org/en/latest/docs/user_guide/tools.html#pointdrawtool )和 PolyEditTool ( https://docs.bokeh.org/en/latest/docs /user_guide/tools.html#PolyEditTool )但似乎不起作用。 任何想法?

这是使用 bokeh 和 networkx 的示例代码:

import networkx as nx

from bokeh.io import output_file, show ,curdoc
from bokeh.models import (BoxZoomTool, Circle, HoverTool,
                          MultiLine, Plot, Range1d, ResetTool,Dropdown,CustomJS)
from bokeh.palettes import Spectral4
from bokeh.plotting import from_networkx
from bokeh.layouts import column


# Prepare Data
G = nx.karate_club_graph()

SAME_CLUB_COLOR, DIFFERENT_CLUB_COLOR = "black", "red"
edge_attrs = {}

for start_node, end_node, _ in G.edges(data=True):
    edge_color = SAME_CLUB_COLOR if G.nodes[start_node]["club"] == G.nodes[end_node]["club"] else DIFFERENT_CLUB_COLOR
    edge_attrs[(start_node, end_node)] = edge_color

nx.set_edge_attributes(G, edge_attrs, "edge_color")


# Show with Bokeh
plot = Plot(plot_width=400, plot_height=400,
            x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
plot.title.text = "Graph Interaction Demonstration"



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

graph_renderer.node_renderer.glyph = Circle(size=15, fill_color=Spectral4[0])
graph_renderer.edge_renderer.glyph = MultiLine(line_color="edge_color", line_alpha=0.8, line_width=1)
plot.renderers.append(graph_renderer)

node_hover_tool = HoverTool(tooltips=[("index", "@index"), ("club", "@club")])
plot.add_tools(node_hover_tool,asd, BoxZoomTool(), ResetTool())

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

我尝试将 PointDrawTool 添加到散景工具中:

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

但它不允许您拖动节点。

我也尝试过使用 PolyDrawTool:

poly_edit_tool = PolyEditTool(renderers=[graph_renderer.node_renderer])
plot.add_tools(node_hover_tool,poly_edit_tool, BoxZoomTool(), ResetTool())

但我得到这个错误:

File "/usr/local/lib/python3.8/dist-packages/bokeh/models/tools.py", line 1510, in _check_compatible_vertex_renderer
    glyph = self.vertex_renderer.glyph
AttributeError: 'NoneType' object has no attribute 'glyph'

似乎目前不支持(Bokeh 2.1.1)。

关于此功能有一个标记为“讨论”的问题: https://github.com/bokeh/bokeh/issues/9399

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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