python - 散景没有图形呈现
问题描述
我正在尝试使用散景包使用 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:字形渲染器中不存在列名,但仍无法使其正常工作。此外,当我从这个问题运行代码时,一个图表呈现没有问题。
任何帮助将不胜感激,干杯。
沙
解决方案
问题是您引用的 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)
推荐阅读
- python - 平均函数返回 INF?
- .net - .Net 禁用表单或禁用鼠标单击,直到它完全加载
- python - 使用 tkinter 中的顶层在另一个模块中的窗口上创建一个新窗口
- json - 如何在 vb.net 中反序列化这个 JSON
- reactjs - 模态正文内容未显示 React,也没有控制台错误
- node.js - Puppeteer - 通过 xPath 获取元素
- regex - Google Colab 使用什么类型的正则表达式来查找和替换功能
- php - 检查关联数组是否为空
- python - 在 Python 中使用 H5T_ARRAY
- apache-spark - 如何使用 udf 函数从数据框中删除特殊字符