我正在使用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)

我希望节点是可拖动的。我想单击一个节点并将其拖动,以便更改其位置。

14/05/2019编辑:
我的进口:
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

最佳答案

来自: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."

如果他们解决了,那就太好了。

关于python - 如何使用NetworkX Spring_Layout获取Bokeh的 'PointDrawTool',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56104789/

10-12 16:45
查看更多