Bokeh RuntimeError,如何为绘图添加工具提示?

Bokeh RuntimeError, how to add tooltips to plot?

我正在尝试将工具提示添加到我之前制作的绘图中:

x 轴是标记位置,y 轴包含基因位置。工具提示目前为空

但是当我尝试添加它们时,出现了运行时错误。

对于绘图,我使用了一个包含标记和基因坐标(分别为 xmarxgen)和 LOD 值的 df。这三列取自三个单独的列表(xmarygenvalue):

DFvalue = pd.DataFrame({'xmar':xmar, 'ygen':ygen, 'value':value})

   xmar  ygen     value
0     0   402  5.075381
1     0   708  4.619449
2     1   489  3.817142
3     1   652  4.396806
4     2   500  3.662211

并有另一个 df 用名称而不是坐标(到 link 到工具提示?)。这个 df 同样由三个列表组成(marnamegennamevalue):

DFname = pd.DataFrame({'xname':marname, 'yname':genname, 'value':value})

      xname      yname     value
0  c1_00593  AT1G05430  5.075381
1  c1_00593  AT1G05900  4.619449
2  c1_00600  AT1G07790  3.817142
3  c1_00600  AT1G08230  4.396806
4  c1_00789  AT1G08920  3.662211

我的绘图代码如下,我猜 ColumnDataSource() 出了点问题,但我不明白为什么或如何?

TOOLS= "hover,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,save"
SOURCE = ColumnDataSource(DFvalue)
TOOLTIPS = [
    ('gene', '@genname'),
    ('marker', '@marname'),
    ('LOD score', '@value')
]

 #Create figure
 p = figure(tools=TOOLS, tooltips=TOOLTIPS)
 p.xaxis.axis_label = 'Position genes'
 p.yaxis.axis_label = 'Position markers'


 p.circle(x=xmar, y=ygen, source=SOURCE, size=6, fill_alpha=0.8)

在 运行 之后我收到以下错误:

p.circle(x=xmar, y=ygen, source=SOURCE, size=6, fill_alpha=0.8)
File "fakesource", line 5, in circle
File "C:\Anaconda3\lib\site-packages\bokeh\plotting\helpers.py", line 757, in func
raise RuntimeError(_GLYPH_SOURCE_MSG % nice_join(incompatible_literal_spec_values, 
conjuction="and"))
RuntimeError: 

Expected x and y to reference fields in the supplied data source.

When a 'source' argument is passed to a glyph method, values that are sequences
(like lists or arrays) must come from references to data columns in the source.

For instance, as an example:

source = ColumnDataSource(data=dict(x=a_list, y=an_array))

p.circle(x='x', y='y', source=source, ...) # pass column names and a source

Alternatively, *all* data sequences may be provided as literals as long as a
source is *not* provided:

p.circle(x=a_list, y=an_array, ...)  # pass actual sequences and no source

错误消息包含有关使用错误的所有信息,以及有关如何解决问题的信息:

Expected x and y to reference fields in the supplied data source.

When a 'source' argument is passed to a glyph method, values that are sequences (like lists or arrays) must come from references to data columns in the source.

For instance, as an example:

source = ColumnDataSource(data=dict(x=a_list, y=an_array))

p.circle(x='x', y='y', source=source, ...) # pass column names and a source

Alternatively, all data sequences may be provided as literals as long as a source is not provided:

p.circle(x=a_list, y=an_array, ...) # pass actual sequences and no source

您将 source 传递给了字形方法,但是您还传递了 x=xmary=ygen 的真实列表。

p.circle(x=xmar, y=ygen, source=SOURCE, size=6, fill_alpha=0.8)

如错误所述,这是不允许的。如果将 source 传递给字形,则字形 h 的 一切都必须来自源 。你不能混搭。因此,您必须将 xmarygen 作为您的 ColumnDataSource 中的列,然后配置 xy 以使用这些列:

p.circle(x='xmar', y='ygen', source=SOURCE, size=6, fill_alpha=0.8)

您可以将这些列 "by hand" 添加到 source.data 字典中,或者您可以在调用 ColumnDataSource(DFvalue)

之前将这些列添加到数据框中

您的 ColumnDataSource 是从 DFvalue 构建的,但您正试图从 DFname 获取工具提示数据。 我认为如果您在 ColumnDataSource:

中包含其他数据
SOURCE = ColumnDataSource(data=dict(
    xmar=DFvalue['xmar'],
    ymar=DFvalue['ygen'],
    value=DFvalue['value']
    marname=DFname['xname']
    genname=DFname['yname']))

您可以在 TOOLTIP 中指向您想要的数据。