在multi_line剧情的Bokeh hovertool

Mar*_*pus 5 python bokeh

我是新手,我只是跳到了使用hovertool,因为这就是为什么我想首先使用散景.现在我正在绘制基因,我想要实现的是具有相同y坐标的多条线,当您将鼠标悬停在一条线上时,您将获得该基因的名称和位置.

我试图模仿这个例子,但由于某种原因,我甚至无法让它显示坐标.

我敢肯定,如果有人真正了解他们的方式看看这个代码,那么错误就会很明显,如果他们向我展示,我会非常感激.

from bokeh.plotting import figure, HBox, output_file, show, VBox, ColumnDataSource
from bokeh.models import Range1d, HoverTool
from collections import OrderedDict
import random

ys = [10 for x in range(len(levelsdf2[(name, 'Start')]))]
xscale = zip(levelsdf2[('Log', 'Start')], levelsdf2[('Log', 'Stop')])
yscale = zip(ys,ys)

TOOLS="pan,wheel_zoom,box_zoom,reset,hover"
output_file("scatter.html")
hover_tips = levelsdf2.index.values
colors = ["#%06x" % random.randint(0,0xFFFFFF) for c in range(len(xscale))]

source = ColumnDataSource(
    data=dict(
        x=xscale,
        y=yscale,
        gene=hover_tips,
        colors=colors,
    )
)


p1 = figure(plot_width=1750, plot_height=950,y_range=[0, 15],tools=TOOLS)
p1.multi_line(xscale[1:10],yscale[1:10], alpha=1, source=source,line_width=10, line_color=colors[1:10])

hover = p1.select(dict(type=HoverTool))
hover.tooltips = [
    ("index", "$index"),
    ("(x,y)", "($x, $y)"),
]

show(p1)
Run Code Online (Sandbox Code Playgroud)

levelsdf2是一个pandas.DataFrame,如果重要的话.

Mar*_*pus 3

我自己想出来了。事实证明,Bokeh 0.8.2 版本不允许对线条使用悬停工具,所以我使用四边形做了同样的事情。

from bokeh.plotting import figure, HBox, output_file, show, VBox, ColumnDataSource
from bokeh.models import Range1d, HoverTool
from collections import OrderedDict
import random


xscale = zip(levelsdf2[('series1', 'Start')], levelsdf2[('series1', 'Stop')])
xscale2 = zip(levelsdf2[('series2', 'Start')], levelsdf2[('series2', 'Stop')])
yscale2 = zip([9.2 for x in range(len(levelsdf2[(name, 'Start')]))],[9.2 for x in range(len(levelsdf2[(name, 'Start')]))])

TOOLS="pan,wheel_zoom,box_zoom,reset,hover"
output_file("linesandquads.html")
hover_tips = levelsdf2.index.values
colors = ["#%06x" % random.randint(0,0xFFFFFF) for c in range(len(xscale))]
proc1 = 'Log'
proc2 = 'MazF2h'
expression1 = levelsdf2[(proc1, 'Level')]
expression2 = levelsdf2[(proc2, 'Level')]
source = ColumnDataSource(
    data=dict(
        start=[min(xscale[x]) for x in range(len(xscale))],
        stop=[max(xscale[x]) for x in range(len(xscale))],
        start2=[min(xscale2[x]) for x in range(len(xscale2))],
        stop2=[max(xscale2[x]) for x in range(len(xscale2))],
        gene=hover_tips,
        colors=colors,
        expression1=expression1,
        expression2=expression2,

    )
)


p1 = figure(plot_width=900, plot_height=500,y_range=[8,10.5],tools=TOOLS)
p1.quad(left="start", right="stop", top=[9.211 for x in range(len(xscale))],
        bottom = [9.209 for x in range(len(xscale))], source=source, color="colors")

p1.multi_line(xscale2,yscale2, source=source, color="colors", line_width=20)


hover = p1.select(dict(type=HoverTool))

hover.tooltips = OrderedDict([
    (proc1+" (start,stop, expression)", "(@start| @stop| @expression1)"),

    ("Gene","@gene"),
])

show(p1)
Run Code Online (Sandbox Code Playgroud)

奇迹般有效。

编辑: 根据要求添加了结果图片,并编辑了代码以匹配发布的屏幕截图。

线条的悬停工具 - 使用四边形的解决方法

这不是最好的解决方案,因为事实证明,在一个图上绘制多个系列的四边形并不那么容易。这可能是可能的,但由于它在我的用例中并不重要,所以我没有进行太多的调查。

由于所有基因都在同一位置的所有系列上表示,因此我只是将所有系列的工具提示添加到四边形,并将其他系列绘制为同一图形上的多线图。

这意味着,如果您将鼠标悬停在 9.21 处的顶行上,您也会获得 9.2 处该行的工具提示,但如果您将鼠标悬停在 9.2 行上,您根本不会获得工具提示。