散景没有图表呈现

问题描述 投票:0回答:1

我正在尝试使用散景包使用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但仍然无法让它工作。此外,当我从这个问题运行代码时,图表没有问题。

任何帮助将不胜感激,欢呼。

python bokeh
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.