首页 > 解决方案 > 散景没有图形呈现

问题描述

我正在尝试使用散景包使用 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:字形渲染器中不存在列名,但仍无法使其正常工作。此外,当我从这个问题运行代码时,一个图表呈现没有问题。

任何帮助将不胜感激,干杯。

标签: pythonbokeh

解决方案


问题是您引用的 ColumnDataSource (条件)中的列不存在。您的代码通过简单地定义条件列表来工作。您的代码中的另一个问题是您定义了两次悬停工具,所以我还通过删除第一个来解决这个问题。

#!/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)

推荐阅读