散景没有呈现图

时间:2019-03-03 17:54:40

标签: python bokeh

我正在尝试通过bokeh软件包使用hoovertool。我有以下代码:

from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import HoverTool

output_file("toolbar.html")

source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[2, 5, 8, 2, 7],
    desc=['A', 'b', 'C', 'd', 'E'],
))

hover = HoverTool()

hover.tooltips = [
    ("index", "@index"),
    ("(x,y)", "(@x, @y)"),
    ("desc", "@desc"),
]

# create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='latitude', y_axis_label='longitude')

# Add circle glyphs to figure p
p.circle(x = 'x', y = 'x', size=10, fill_color="grey", line_color=None, 
         hover_fill_color="condition", hover_line_color="white", source = source)

# Create a HoverTool: hover
hover = HoverTool(tooltips=None, mode='vline')

# Add the hover tool to the figure p
p.add_tools(hover)

# Specify the name of the output file and show the result
output_file('hover_glyph.html')
show(p)

代码运行时将打开一个新选项卡,但没有图形。我尝试过推杆。

x = [1, 2, 3, 4, 5]; y = [2, 5, 8, 2, 7]
p.circle(x = 'x', y = 'x', size=10, fill_color="grey", line_color=None, 
         hover_fill_color="condition", hover_line_color="white", source = source)

我也看过这些先前的问题。 Jupyter Bokeh: Non-existent column name in glyph renderer,但仍然无法正常使用。另外,当我从这个问题运行代码时,图形也没有问题。

任何帮助,不胜感激。

桑迪

1 个答案:

答案 0 :(得分:1)

问题是您所引用的ColumnDataSource中的列(条件)不存在。您的代码只需定义条件列表即可工作。您代码中的另一个问题是您定义了两次hovertool,因此我也通过删除第一个来解决了这个问题。

#!/usr/bin/python3
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import HoverTool

output_file("toolbar.html")

source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[2, 5, 8, 2, 7],
    desc=['A', 'b', 'C', 'd', 'E'],
    condition=['red', 'blue', 'pink', 'purple', 'grey']
))

# create a new plot with a title and axis labels
p = figure(title="simple line example", x_axis_label='latitude', y_axis_label='longitude')

# Add circle glyphs to figure p
p.circle(x = 'x', y = 'y', size=10, fill_color="grey", line_color=None, hover_fill_color="condition", hover_line_color="white", source = source)

hover = HoverTool(mode='vline')

hover.tooltips = [
    ("(x,y)", "(@x, @y)"),
    ("desc", "@desc")
]

# Add the hover tool to the figure p
p.add_tools(hover)

# Specify the name of the output file and show the result
output_file('hover_glyph.html')
show(p)