Stefan Verweij
Stefan Verweij

Reputation: 172

Bokeh RuntimeError, how to add tooltips to plot?

I am trying to add tooltips to a plot I have made earlier:

enter image description here

On the x-axis are the marker positions, the y-axis contains the gene positions. The tool tips are currently empty

But when I try to add them I get a RuntimeError.

For the plotting I use a df which contains the marker and gene coordinates (respectively xmar and xgen) and the LOD values. These three columns are taken from three separate lists (xmar, ygen and value):

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

and have another df with names instead of coordinates (to link to the tooltips?). This df again is made from three lists (marname, genname and value):

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

My code for the plotting itself is as following and I guess something goes wrong with the ColumnDataSource() but I cannot see why or how?

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)

after running I receive the following error:

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

Upvotes: 1

Views: 2035

Answers (2)

Pallie
Pallie

Reputation: 1099

Your ColumnDataSource is built from DFvalue but you are trying to get the tooltip data from DFname. I think if you include the other data in your ColumnDataSource:

SOURCE = ColumnDataSource(data=dict(
    xmar=DFvalue['xmar'],
    ymar=DFvalue['ygen'],
    value=DFvalue['value']
    marname=DFname['xname']
    genname=DFname['yname']))

You can point to the data you wish in TOOLTIP.

Upvotes: 1

bigreddot
bigreddot

Reputation: 34568

The error message contains all the information about the usage error, as well as information about how to fix things:

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

You passed source to the glyph method, But you also passed real lists for x=xmar and y=ygen.

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

As the error states, that's not permissible. If you pass source to a glyph, the everything for the glyp hmust come from the source. You can't mix and match. So, you must put xmar and ygen as columns in your ColumnDataSource, then, configure x and y to use those columns:

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

You could add these columns "by hand" to the source.data dictionary, or you could add columns to your dataframe before calling ColumnDataSource(DFvalue)

Upvotes: 0

Related Questions