散景:HoverTool()ColumnDataSource显示???使用@时 - python

我想将工具提示添加到ColumDataSource(),使其紧靠最近的数据点。但是使用@x, @y时会显示???而不是最接近的值。
使用$x, $y可以正常工作。

下面提供了一个示例:

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

a = [x for x in range(10)]
b = [x for x in range(10)]
c = [0.5 * x for x in range(10)]

source = ColumnDataSource(data=dict(a=a, b=b, c=c))

p = figure()
p.line(x='a', y='b', source=source)
p.line(x='a', y='c', source=source)

p.add_tools(HoverTool(
    tooltips=[
        ('index', '$index'),
        ('($x, $y)', "($x, $y)"),
        ('(@x, @y)', "(@x, @y)"),
        ('(@a, @b, @c)', "(@a, @b, @c)")],
    line_policy='nearest',
    mode='mouse'))

show(p)

结果

散景:HoverTool()ColumnDataSource显示???使用@时 - python

当我直接通过列表时,它可以正常工作…

在带有两个图形的图中,我只想显示当前悬停图形的最近值。因此,使用@b, @c不是我想要的。

更新:

该图有两个图形,我只想显示悬停的图形的y轴的值。

我想要的结果是:

散景:HoverTool()ColumnDataSource显示???使用@时 - python

但是在这种情况下,我直接传递了列表对象:

p.line(a, b)
p.line(a, c)

p.add_tools(HoverTool(
    tooltips=[
        ('index', '$index'),
        ('(@x, @y)', "(@x, @y)")],
    line_policy='nearest',
    mode='vline'))

使用ColumnDataSource()时,我必须使用变量的名称,并且不能使用@y引用y轴。

因此,我达到了以下结果:

散景:HoverTool()ColumnDataSource显示???使用@时 - python

p.line(x='a', y='b', source=source)
p.line(x='a', y='c', source=source)

p.add_tools(HoverTool(
    tooltips=[
        ('index', '$index'),
        ('(@x, @y)', "(@x, @y)"),
        ('@a', '@a'),
        ('@b', '@b'),
        ('@c', '@c')],
    line_policy='nearest',
    mode='vline'))

悬停工具不会仅显示悬停图形的y轴值。它显示两个值(@b and @c)。

参考方案

您必须在ColumnDataSource中设置要显示的值。我不太了解您要显示的内容,但是我将粘贴Bokeh中的示例代码,您可以在页面中找到该代码。
基本上,“ x”和“ y”是要绘制的变量,下一个是要显示的变量。

# Make the ColumnDataSource: source
source = ColumnDataSource(data={
    'x'       : data.loc[1970].fertility,
    'y'       : data.loc[1970].life,
    'country' : data.loc[1970].Country,
})

# Create the figure: p
p = figure(title='1970', x_axis_label='Fertility (children per woman)', 
y_axis_label='Life Expectancy (years)',plot_height=400, plot_width=700,
tools=[HoverTool(tooltips='@country')])

# Add a circle glyph to the figure p
p.circle(x='x', y='y', source=source)

# Output the file and show the figure
output_file('gapminder.html')
show(p) 

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…

TypeError:'str'对象不支持项目分配,带有json文件的python - python

以下是我的代码import json with open('johns.json', 'r') as q: l = q.read() data = json.loads(l) data['john'] = '{}' data['john']['use…