Is it possible to do on-the-fly math with ColumnDataSource columns? In, e.g., matplotlib, if I have numpy arrays x
and y
, I could do something like plt.plot(x, y+5)
.
I’m working in bokeh and I’d like to draw a quad whose x location is specified by a column ‘X’ in my ColumnDataSource. But if I try something like this:
p = figure(title="My plot")
# df is a pandas dataframe
cds = ColumnDataSource(df)
p.quad(source=cds, bottom='START_INDEX', top='END_INDEX', left='X'-0.5,
right='X'+0.5, color='blue', alpha=0.01)
then I get the error
TypeError: unsupported operand type(s) for -: 'str' and 'float'
because it thinks I am trying to subtract 0.5 from the string ‘X’ rather than the column named ‘X’.
On the other hand, if I do this:
p.quad(source=cds, bottom='START_INDEX', top='END_INDEX', left=cds.data['X']-0.5,
right=cds.data['X']+0.5, color='blue', alpha=0.01)
I no longer get the TypeError, but instead I get a RuntimeError about mixing data sources:
RuntimeError:
Expected left and right to reference fields in the supplied data source.
Of course I can add new columns to my ColumnDataSource that are X - 0.5 and X + 0.5, but that involves some duplication of data.
Any simple trick I’m missing here? In truth it’s not a big deal since my data source in this example isn’t huge, but I’m curious what the best idiom is in cases like this.
Thanks in advance for any thoughts!