除非有另一个情节,否则散景不会划分细分

时间:2015-09-15 09:46:40

标签: python pandas bokeh

我可能在这里忽略了显而易见的事,但我对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)

我得到了下图:

Bad plot

现在,如果我在显示图形之前添加圆形图,它就会起作用。

# p.segment...

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

show(p)

Good plot

1 个答案:

答案 0 :(得分:1)

我认为您遗失的是x_range实例化中的figure关键字参数:

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)

所以这应该适合你,而不必在情节中添加圆形字形。

尽管如此,我仍然不知道为什么在添加x_range字形时它需要segment() kwarg,而不是圆形字形。