散景不绘制片段,除非有另一个情节

Bokeh not plotting segments, unless there is another plot

我可能在这里遗漏了明显的东西,但我对 Bokeh 有一个非常奇怪的行为。

假设我有以下 Pandas 数据框:

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)

现在我尝试从中绘制片段:

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)

得到下图:

现在,如果我在显示图形之前添加圆图,就可以了。

# p.segment...

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

show(p)

我认为您所缺少的只是 figure 实例化中的 x_range 关键字参数:

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)

所以这应该对你有用,而不必在图中添加一个圆圈字形。

就是说,我仍然不知道为什么在添加 segment() 字形时需要 x_range kwarg,而不是圆形字形。