Plot a pandas dataframe using the dataframe index for x coordinate in bokeh

Krastanov picture Krastanov · Jun 19, 2016 · Viewed 12.5k times · Source

I want to prepare a bokeh plot that uses a ColumnDataSource. The pandas DataFrame that is the source of the data has one column and a datetime index:

enter image description here

How do I specify that the x value should be the index. I tried just omitting it, hoping that would be the default, but it did not work:

enter image description here

There is an ugly solution where I just copy the index as a column in the dataframe, but I hope there is a more elegant solution:

enter image description here enter image description here

Answer

Luke Canavan picture Luke Canavan · Jun 30, 2016

The issue is that you have to specify which column should be the "x" column. If you don't specify the "x" value, the default behavior in bokeh.plotting is to try to find a column called "x" in your ColumnDataSource (which doesn't exist).

One tricky thing here is that you're using a named index ('timeseries') in pandas. That name is carried over when you create a ColumnDataSource, so that your source probably looks like:

ds = ColumnDataSource(df)
print(ds.data)
# the ts_n values would be the actual timestamps from the df
> {'timestamp': [ts_1, ts_2, ts_3, ts_4, ts_5], 'avg': [0.9, 0.8, 0.7, 0.8, 0.9]}

It would work if you use:

p.line(source=ds, x='timestamps', y='avg')