我一直在使用以下代码来可视化bokeh中的networkx网络...
import networkx as nx
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from bokeh.plotting import figure, ColumnDataSource
from bokeh.models.sources import CDSView
from bokeh.io import show, output_notebook
from bokeh.models import Plot, Range1d, MultiLine, Circle, HoverTool, BoxZoomTool, ResetTool
from bokeh.models.graphs import from_networkx, NodesAndLinkedEdges, EdgesAndLinkedNodes, NodesOnly
from bokeh.palettes import *
V = [1,2,3,4,5,6]
E = [(1,2), (4,5), (2,3)]
G=nx.Graph()
G.add_nodes_from(V)
G.add_edges_from(E)
plot = figure(plot_width=500, plot_height=500,
x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
plot.title.text = "Networkx/Bokeh Example"
plot.axis.visible = None
plot.background_fill_color = "white"
plot.grid.grid_line_color = None
network_src = dict()
network_src['index'] = V
network_src['name'] = ['one', 'two', 'three', 'four', 'five', 'six']
network_src['size'] = [15,15,15,30,15,30]
network_src['node_cols'] = ['#2b83ba','#2b83ba','#2b83ba','#abdda4','#abdda4','#fdae61']
network_src = ColumnDataSource(network_src)
graph_renderer = from_networkx(G, nx.spring_layout, scale=1, center=(0, 0))
graph_renderer.node_renderer.glyph = Circle(size='size', fill_color='node_cols')
graph_renderer.node_renderer.data_source = network_src
graph_renderer.node_renderer.view = CDSView(source=network_src)
node_hover_tool = HoverTool(tooltips=[("Name", "@name")])
plot.add_tools(node_hover_tool)
plot.renderers.append(graph_renderer)
output_notebook()
show(plot)
这很好用,但是当我将鼠标悬停在某些节点上时,它在方框周围显示了多个箭头。我已经读到这是因为它试图将悬停工具提示分配给边缘和节点渲染器...但是我已经尝试对其进行修复,而且似乎找不到正确的解决方案。请帮忙!谢谢。
答案 0 :(得分:1)
Looks like a bug. Please report it here. For now you could use:
node_hover_tool = HoverTool(tooltips = [("Name", "@name")], show_arrow = False)