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

14,097

Solution 1

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')

Solution 2

I usually reset the index and this makes the index a column. Similar to your ugly solution. Then plot the specified columns.

df.reset_index(inplace = True)

Alternatively you could reference just the column and in matplotlib it usually uses the index by default in the way you want. Not sure if it will work for you but worth a try.

df["avg"].plot()

Alternatively you could try the time series plot approach? Detailed below.

TimeSeries in Bokeh using a dataframe with index

Solution 3

You can call the index with the usual syntax to get an index from DF as:
p.line(x = df.index.values, y = df['values_for_y'])

Share:
14,097
Krastanov
Author by

Krastanov

Postdoc in Quantum Information at MIT. Open Source and Scientific Computing enthusiast.

Updated on June 14, 2022

Comments

  • Krastanov
    Krastanov almost 2 years

    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

  • Krastanov
    Krastanov almost 8 years
    I see, I did not know that indices (as opposed to normal columns) can be named. Thanks.
  • Krastanov
    Krastanov over 6 years
    But then you are not using an explicit common dataframe (the source keyword argument), which complicates the fancier interactive multi-axis figures you would want to build on top of simple subplots.