散景:将HoverTool工具提示与GraphRenderer节点和边线相结合

时间:2018-08-16 13:24:47

标签: python bokeh

在下面的代码中,我创建了带有指示列标签的DataFrame。数据以圆形图的形式绘制,标签对应于DataFrame列“ Abbrev”。

我希望HoverTool工具提示显示:给定节点及其连接的节点的“ Abbrev”,“ Month”,“ Number”。我一直无法执行此操作,因为我只能显示??? s。

output_file('bokeh4.html')

# create data
df = pd.DataFrame()
df['Abbrev'] = ['JAN','FEB','MAR','APR','MAY','JUN']
df['Month'] = ['January','February','March','April','May','June']
df['Number'] = np.arange(1,7,1)
df['Color'] = ['blue','red','green','purple','orange','black']
num_of_pts = df.shape[0]

# create node positions and edges
node_indices = list(df.index)
node_colors = df['Color'].values
radius = 1.5  # for nodes
x = []
y = []
theta = 0.5*np.pi  # start at 12 o'clock
start_node= []
end_node = []
for i in range(num_of_pts):
    x.append(radius*np.cos(theta))
    y.append(radius*np.sin(theta))
    theta = theta - (2.0*np.pi/num_of_pts)  # move clockwise
    for j in range(num_of_pts):
        if i < j:
            if (j - i) == 2:
                start_node.append(i)
                end_node.append(j)

df['x'] = x    
df['y'] = y

# add Abbrevs as labels
source = ColumnDataSource(df)
labels = LabelSet(x='x', y='y', text='Abbrev', x_offset=3, y_offset=3, source=source)

plot = figure(x_range=(-1.75,1.75), y_range=(-1.75,1.75),
              tools='reset,pan,zoom_in,zoom_out,wheel_zoom,box_zoom,hover,save',
              toolbar_location='right',
              tooltips=[('Abbrev','@Abbrev'),('Month','@Month'),('Number','@Number')])

plot.title.text = 'Bokeh4'
plot.title.align = 'center'
plot.title.text_font_size = '20px'

plot.xaxis.visible = False
plot.xgrid.visible = False
plot.yaxis.visible = False
plot.ygrid.visible = False

plot.renderers.append(labels)

graph = GraphRenderer()

circle_size = 9

graph.node_renderer.data_source.add(node_indices, 'index')
graph.node_renderer.data_source.add(node_colors, 'color')

graph.node_renderer.glyph = Circle(size=circle_size, fill_color="color")

graph.edge_renderer.data_source.data = dict(start=start_node, end=end_node)

graph_layout = dict(zip(node_indices, zip(x, y)))
graph.layout_provider = StaticLayoutProvider(graph_layout=graph_layout)


graph.node_renderer.selection_glyph = Circle(size=circle_size, fill_color=RdYlBu10[0])
graph.node_renderer.hover_glyph = Circle(size=circle_size, fill_color=RdYlBu10[0])

graph.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=1.5)
graph.edge_renderer.selection_glyph = MultiLine(line_color=RdYlBu10[8], line_width=1.5)
graph.edge_renderer.hover_glyph = MultiLine(line_color=RdYlBu10[8], line_width=1.5)

graph.selection_policy = NodesAndLinkedEdges()
graph.inspection_policy = EdgesAndLinkedNodes()

plot.renderers.append(graph)

save(plot)
del plot

0 个答案:

没有答案