在multi_line剧情的Bokeh hovertool

时间:2015-05-13 19:16:08

标签: python bokeh

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

我试图模仿this示例,但由于某种原因,我甚至无法显示坐标。

我确信,如果真正了解散景的人看到这段代码,那么错误就会很明显,如果他们向我展示,我会非常感激。

NameOfYourApp::Application.load_tasks

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) 是一个pandas.DataFrame,如果重要的话。

1 个答案:

答案 0 :(得分:3)

我自己想通了。事实证明,Bokeh的0.8.2版本不允许使用hovertool,所以我使用四边形做了同样的事情。

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)

像魅力一样工作。

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

Hovertool for lines - workaround using quads

这不是最好的解决方案,因为事实证明在一个地块上绘制几个系列的四边形并不是那么容易。这可能是可能的,但在我的用例中并没有多大关系,我没有过于激烈地调查。

由于所有基因都出现在同一个地方的所有系列中,我只是将所有系列的工具提示添加到四边形中,并将另一个系列绘制为同一图上的多线图。

这意味着如果你在9.21的顶线上徘徊,你也可以在9.2获得该行的工具提示,但如果你在9.2行上徘徊,你根本就不会得到工具提示。

相关问题