【问题标题】:Drag nodes using Networkx and Bokeh使用 Networkx 和 Bokeh 拖动节点
【发布时间】:2020-11-07 00:43:05
【问题描述】:

我正在使用带有 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'

【问题讨论】:

  • 我们需要一个最小的示例,您可以在其中试用这些工具但它们不起作用。

标签: python graph visualization bokeh networkx


【解决方案1】:

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

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

【讨论】:

  • 谢谢,不知道有没有其他的拖节点选项?
  • 我可以看到两个选项:1) 帮助解决问题和 2) 手动实现图表,以便节点是常规字形。
猜你喜欢
  • 1970-01-01
  • 2019-08-24
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-27
  • 2019-09-28
  • 2019-11-27
相关资源
最近更新 更多