散景图未显示

时间:2019-11-19 11:02:15

标签: python plot visualization bokeh

我正在尝试使用堆叠的水平条生成甘特图样式的图,但是当我显示()图时,页面为空白,并且在js控制台中遇到的错误是SCRIPT5007: Unable to get property 'length' of undefined or null reference。我可以查看其他散景图,例如散景文档中堆积的竖线示例。

这是我用来创建情节的代码

from bokeh.plotting import figure, show, output_notebook, output_file
from bokeh.models import ColumnDataSource, Range1d

grouped=df.groupby('Org')
source=ColumnDataSource(grouped)

p=figure(title='Project Schedule', x_axis_type='datetime', y_range=grouped['Org'],
    x_range=Range1d(df.Start.min(),df.Finish.max()))



p.hbar_stack('Task', y='Org', height=0.9, source=source)

show(p)

以及我使用的数据样本(在熊猫数据框中)

   Org Task      Start     Finish
0   o1   t1 2019-11-19 2019-11-23
1   o1   t2 2019-11-23 2019-11-27
2   o1   t3 2019-11-27 2019-12-01
3   o1   t4 2019-12-02 2019-12-06
4   o2   t1 2019-11-20 2019-11-24
5   o2   t2 2019-11-24 2019-11-28
6   o2   t3 2019-12-01 2019-12-05
7   o2   t4 2019-12-05 2019-12-09
8   o3   t1 2019-11-20 2019-11-24
9   o3   t2 2019-11-24 2019-11-28
10  o3   t3 2019-12-01 2019-12-05
11  o3   t4 2019-12-05 2019-12-09

1 个答案:

答案 0 :(得分:0)

您可以这样做(Bokeh v1.3.0):

from bokeh.plotting import figure, show, output_notebook, output_file
from bokeh.models import ColumnDataSource, Range1d
from bokeh.core.properties import value
import pandas as pd

df = pd.DataFrame({'Org': ['o1', 'o1','o1','o1', 'o2','o2','o2','o2','o3','o3','o3','o3'],
                   'Task': ['t1', 't2','t3','t4', 't1', 't2','t3','t4','t1', 't2','t3','t4'],
                   'Start': pd.date_range('2019-02-24', periods=4*3, freq='D'),
                   'Finish': pd.date_range('2019-03-24', periods=4*3, freq='D')})
df['Diff'] = df['Finish'] - df['Start']

x = df['Task'].unique().tolist()
y = df['Org'].unique().tolist()

data1 = {'y': y}
data2 = {t: [df['Diff'][j + i % 4] for j in range(len(y))] for i, t in enumerate(x)}
data = {**data1, **data2}

p = figure(title='Project Schedule', y_range = y, x_axis_type='datetime')
p.hbar_stack(x, y='y', height=0.9, color=['red', 'blue', 'orange', 'green'], legend=[value(k) for k in x], source=data)

show(p)

但是它不能很好地工作,我需要更多的逻辑来计算以毫秒为单位的时间差...

enter image description here