Romain Endelin
Romain Endelin

Reputation: 148

Bokeh not plotting segments, unless there is another plot

I am probably missing the obvious here, but I'm having a very strange behaviour with Bokeh.

Let's say I have the following Pandas dataframe:

import pandas as pd

test = pd.DataFrame({'date': [pd.Timestamp('2014-11-24 17:18:28'),
                          pd.Timestamp('2014-11-24 17:18:38'),
                          pd.Timestamp('2014-11-24 17:18:48'),
                          pd.Timestamp('2014-11-24 17:19:21'),
                          pd.Timestamp('2014-11-24 17:19:41'),
                          pd.Timestamp('2014-11-24 17:19:49'),
                          pd.Timestamp('2014-11-24 17:19:59'),
                          pd.Timestamp('2014-11-24 17:25:43'),
                          pd.Timestamp('2014-11-24 17:25:53'),
                          pd.Timestamp('2014-11-24 17:25:58'),
                          pd.Timestamp('2014-11-24 17:26:08'),
                          pd.Timestamp('2014-11-24 17:26:18'),
                          pd.Timestamp('2014-11-24 17:26:28'),
                          pd.Timestamp('2014-11-24 17:26:31'),
                          pd.Timestamp('2014-11-24 17:26:39'),
                          pd.Timestamp('2014-11-24 17:26:43'),
                          pd.Timestamp('2014-11-24 17:26:53'),
                          pd.Timestamp('2014-11-24 17:27:01'),
                          pd.Timestamp('2014-11-24 17:27:06'),
                          pd.Timestamp('2014-11-24 17:27:09')],
                 'activity': ['occupied', 'occupied', 'sleep',
                             'sleep', 'sleep', 'occupied',
                             'sleep', 'occupied', 'occupied',
                             'sleep', 'sleep', 'occupied',
                             'occupied', 'sleep', 'occupied',
                             'occupied', 'occupied', 'occupied',
                             'occupied', 'occupied'],
                 'since': [pd.Timestamp('2014-11-24 17:18:28'),
                             pd.Timestamp('2014-11-24 17:18:38'),
                             pd.Timestamp('2014-11-24 17:18:48'),
                             pd.Timestamp('2014-11-24 17:18:58'),
                             pd.Timestamp('2014-11-24 17:18:58'),
                             pd.Timestamp('2014-11-24 17:19:49'),
                             pd.Timestamp('2014-11-24 17:19:59'),
                             pd.Timestamp('2014-11-24 17:20:06'),
                             pd.Timestamp('2014-11-24 17:20:06'),
                             pd.Timestamp('2014-11-24 17:25:58'),
                             pd.Timestamp('2014-11-24 17:26:08'),
                             pd.Timestamp('2014-11-24 17:26:18'),
                             pd.Timestamp('2014-11-24 17:26:28'),
                             pd.Timestamp('2014-11-24 17:26:31'),
                             pd.Timestamp('2014-11-24 17:26:39'),
                             pd.Timestamp('2014-11-24 17:26:39'),
                             pd.Timestamp('2014-11-24 17:26:39'),
                             pd.Timestamp('2014-11-24 17:26:39'),
                             pd.Timestamp('2014-11-24 17:26:39'),
                             pd.Timestamp('2014-11-24 17:26:39')]})
test.set_index('date', inplace=True)

Now I try to plot segments from it:

from bokeh.plotting import figure, show

p = figure(width=1500, height=400,
       title="Test",
       x_axis_label="Date", x_axis_type="datetime",
       y_axis_label="Activities", y_range=list(test.activity.unique()),
       tools="box_select,xpan,xwheel_zoom,reset,save")

p.segment(x0=test.since, y0=test.activity,
      x1=test.index.get_level_values('date'), y1=test.activity)

show(p)

I get the following figure:

Bad plot

Now, if I add circle plots before showing the figure, it works.

# p.segment...

p.circle(x=test.since,
     y=test.activity)

show(p)

Good plot

Upvotes: 0

Views: 506

Answers (1)

dagrha
dagrha

Reputation: 2559

I think all you are missing is the x_range keyword argument in your figure instantiation:

from bokeh.plotting import figure, show

p = figure(width=1000, height=400,
       title="Test",
       x_axis_label="Date", x_axis_type="datetime", 
  -->  x_range=(test.index.min(), test.index.max()),
       y_axis_label="Activities", y_range=list(test.activity.unique()),
       tools="box_select,xpan,xwheel_zoom,reset,save")

p.segment(x0=test.since, y0=test.activity,
      x1=test.index, y1=test.activity)

show(p)

So that should work for you without having to add a circle glyph to the plot.

That said, I still don't know why it needs the x_range kwarg when adding a segment() glyph, but not for the circle glyph.

Upvotes: 2

Related Questions