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

2024-06-09 03:12:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

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

但是当我试图添加它们时,我得到了一个运行时错误。在

对于绘图,我使用包含标记和基因坐标(分别是xmar和{})和LOD值的df。这三列来自三个单独的列表(xmarygen和{}):

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的名称而不是坐标(链接到工具提示吗?)。这个df又是由三个列表组成的(marnamegenname和{}):

^{pr2}$

我的绘图代码如下所示,我猜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

Tags: and工具in绘图sourcedfdatavalue
2条回答

错误消息包含有关使用错误的所有信息,以及有关如何修复的信息:

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传递给glyph方法,但也为x=xmar和{}传递了实际列表。

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

正如错误所说,这是不允许的。如果将source传递给glyp,glyp的所有内容都必须来自源代码。你不能混搭。因此,您必须将xmar和{}作为列放在ColumnDataSource中,然后配置x和{}以使用这些列:

^{pr2}$

您可以“手动”将这些列添加到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中指向所需的数据。

相关问题 更多 >