如何在使用NetworkX Spring_Layout时使Bokeh的'PointDrawTool'工作

7
我正在使用NetworkX和Bokeh制作网络节点图。我正在使用NetworkX的“spring_layout”自动为每个节点生成位置。然而,我无法弄清楚如何在我的图形上拖动节点(并使边跟随任何拖动节点移动)。
如何为我的NetworkX/Bokeh图启用节点拖动功能?
我尝试使用Bokeh的“PointDrawTool”工具,但是即使该工具在我的图形旁边的工具栏中呈现和显示,它也无法正常工作。
plot = Plot(plot_width=1000, plot_height=1000,
            x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1))


plot.title.text = "Network Graph"

graph_renderer = from_networkx(G, nx.spring_layout)

plot.add_tools(HoverTool(tooltips=[("ID", "@index"), ("Internal IP", "@Internal")]), PointDrawTool(renderers = [graph_renderer.node_renderer], empty_value = 'black'), TapTool(), BoxSelectTool(), BoxEditTool(), BoxZoomTool(), PanTool(), WheelZoomTool(), ZoomInTool(), ZoomOutTool(), SaveTool(), UndoTool())

graph_renderer.node_renderer.glyph = Circle(size=10, fill_color=Spectral4[0])
graph_renderer.node_renderer.selection_glyph = Circle(size=10, fill_color=Spectral4[2])
graph_renderer.node_renderer.hover_glyph = Circle(size=10, fill_color=Spectral4[1])

graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=1)
graph_renderer.edge_renderer.selection_glyph = MultiLine(line_color=Spectral4[2], line_width=3)
graph_renderer.edge_renderer.hover_glyph = MultiLine(line_color=Spectral4[1], line_width=3)

graph_renderer.selection_policy = NodesAndLinkedEdges()
graph_renderer.inspection_policy = NodesAndLinkedEdges()

plot.renderers.append(graph_renderer)

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

我希望这些节点可以被拖动。我想点击一个节点并拖动它,以改变其位置。 2019年5月14日编辑: 我的引入:
import pandas as pd
import numpy as np
import networkx as nx
from bokeh.io import show, output_file
from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, TapTool, BoxEditTool, BoxSelectTool, BoxZoomTool, ResetTool, PanTool, WheelZoomTool, ZoomInTool, ZoomOutTool, SaveTool, UndoTool, PointDrawTool
from bokeh.models.graphs import from_networkx, NodesAndLinkedEdges, EdgesAndLinkedNodes
from bokeh.palettes import Spectral4
import warnings
import matplotlib.pyplot as plt
%matplotlib notebook

from IPython.display import display, HTML
from IPython.core.interactiveshell import InteractiveShell

你能添加一下你的导入代码块吗?目前还不清楚你是如何导入 bokeh 的子模块的。 - vurmux
@vumux,我已经更新了我的帖子,并附上了所有的导入内容。 - Isabel
1
我很少使用Bokeh,但是在他们的演示文稿中没有任何迹象表明他们已经实现了拖动节点周围所需的逻辑。我不知道您是否坚决要使用Bokeh,但我编写了一个支持拖动节点和边缘的小型图形可视化库,名为netgraph,您可以在这里找到它。非常希望听到您的反馈,特别是如果它不能满足您的需求。;-) - Paul Brodersen
1个回答

0

来自: https://discourse.bokeh.org/t/networkx-import-and-pointdrawtool/3512

"The proximate cause of the problem is that the tool requires access to
 glyph.x.field and glyph.y.field but both of these are null for the Circle node
 renderer. The GraphRenderer is the only example of a “composite” renderer, so it’s
 entirely possible (probable) that there is some interaction problem specific to it
 with the PointDrawTool. The next appropriate step would be to make a bug report
 issue on GitHub 5 with details."

如果他们解决了这个问题,那就太好了。


请您提供样例代码以便更好地理解。 - Varun Jain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接