HoverTool用于散景散点图中的多个数据系列 [英] HoverTool for multiple data series in bokeh scatter plot

查看:170
本文介绍了HoverTool用于散景散点图中的多个数据系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下使用numpy和bokeh的小示例脚本:

I have the following small example script making use of numpy and bokeh:

import numpy as np
import bokeh.plotting as bp
from bokeh.objects import HoverTool 
bp.output_file('test.html')

fig = bp.figure(tools="reset,hover")
x = np.linspace(0,2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)
s1 = fig.scatter(x=x,y=y1,color='#0000ff',size=10,legend='sine')
s1.select(dict(type=HoverTool)).tooltips = {"x":"$x", "y":"$y"}
s2 = fig.scatter(x=x,y=y2,color='#ff0000',size=10,legend='cosine')
s2.select(dict(type=HoverTool)).tooltips = {"x":"$x", "y":"$y"}
bp.show()

问题在于,悬停工具仅适用于余弦曲线,而不适用于正弦曲线.

The problem is that the hover tool only works for the cosine curve but not for the sine.

我知道一种选择是同时绘制两个序列并更改余弦数据点的颜色:

I know that one option would be to plot both series togehter and change the color of the cosine data points:

import numpy as np
import bokeh.plotting as bp
from bokeh.objects import HoverTool 
bp.output_file('test.html')

fig = bp.figure(tools="reset,hover")
x = np.linspace(0,2*np.pi)

y1 = np.sin(x)
y2 = np.cos(x)

x = np.array([x,x]).flatten()
y = np.array([y1,y2]).flatten()

blue = np.array('#0000ff').flatten()
red = np.array('#ff0000').flatten()
colors = np.array([blue.repeat(len(y1)),red.repeat(len(y1))]).flatten()

s1 = fig.scatter(x=x,y=y,color=colors,size=10,legend='sine & cosine')
s1.select(dict(type=HoverTool)).tooltips = {"x":"$x", "y":"$y"}
bp.show()

但随后我松开了第二种颜色的图例条目.

But then I loose the legend entry for the second color.

如何设法将鼠标悬停在两个数据集上并查看相应的工具提示?

How do I manage to be able to hover over both data sets and see the corresponding tooltip?

谢谢!

最大

推荐答案

最初的答案是古老而过时的,这是如何使用任何现代版本的Bokeh来实现这一点:

The original answer was ancient and outdated, here is how to accomplish this with any modern version of Bokeh:

from bokeh.plotting import figure, show
import numpy as np

x = np.linspace(0, 2*np.pi)
y1 = np.sin(x)
y2 = np.cos(x)

fig = figure(tools="reset", tooltips=[("x", "$x"), ("y", "$y")])
s1 = fig.scatter(x, y1, color='#0000ff', size=10, legend_label='sine')
s2 = fig.scatter(x, y2, color='#ff0000', size=10, legend_label='cosine')

show(fig)

这篇关于HoverTool用于散景散点图中的多个数据系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆