1

Im trying to build an application in which I need to extract the x,y value of a bokeh line. Im able to do this for a bokeh circle (see below, where I find the x value of the circle is tmp1.glyph.x = 2), but the same syntax doesnt work for a line between two points (tmp1.glyph.x ="x"). I would hope to see [-3,3]. Would be grateful for any advice.

from bokeh.plotting import figure,  show


fig = figure(x_range=(-5,5),y_range=(-5, 5))
tmp1=fig.circle(x=2, y=-3, size=5)
tmp=fig.line(x = [-3,3], y = [4,-4])
print(tmp1.glyph.x)
# output: 2
print(tmp.glyph.x)
# output: x

show(fig)
AlexSG
  • 11
  • 1

1 Answers1

1

For the line glyph a ColumnDataSource object is created. To print the data of this ColumnDataSource use tmp.data_source.data['x'] in your example.

To explain this behavior in more detail, you have to know, that if you pass only one value for x and y for a glyph, this value is stored directly as value (inside the object is looks like this: x = {'value': 2}). If you pass a list to the glyph this gets a pointer with the name of the column in the ColumnDataSource (inside it looks like this x = {'field': 'x'}). The same behavior has the circle glyph, you can try it out adding one value as a list.

Therefor a general solution to print the values could look like the code below:

value = tmp.glyph.x
if isinstance(field_or_value, str):
    value = tmp1.data_source.data[value]
print(value)

Here we check if the value in tmp.glyph.x is a string. If it is a string, this is a pointer the the ColumnDataSource.

mosc9575
  • 5,618
  • 2
  • 9
  • 32