如何加快 Bokeh 中创建的绘图的页面加载速度?

Sam*_*haw 5 python plot interactive python-3.x bokeh

运行代码后,页面打开,空白页面显示 13 秒,之后出现预期的绘图。

有没有办法加快这个速度?(13 秒,对于用户来说太长了)

该代码生成 44 个图,分为两列(2n 个循环),按参数不同(第一个循环),然后按工具名称不同(第 3 个循环)

另外,如果 bokeh 不是合适的工具,我会很高兴听到一种绘制交互式图的方法,最好使用 python

这是代码:

dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%dT%H:%M:%S')

df = pd.read_csv("LUSU.csv",parse_dates=['PM_START_DATE'], date_parser=dateparse)

df.head()
print("time elapsed: {:.2f}s".format(time.time() - start_time))
x = df['PM_START_DATE']
y = df['TASK_VALUE']
tool=df['ENTITY']
tool_list=df['ENTITY'].unique()
param_list=df['PARAMETER'].unique()
#
print("time elapsed: {:.2f}s".format(time.time() - start_time))
colors = itertools.cycle(Spectral11)
output_file('LUSU.html', mode="cdn")
for i in range(0,44,2):
    row = []
    for _ in range(2):
        p = figure(title=param_list[i], x_axis_label='date', y_axis_label='chart value', x_axis_type="datetime", toolbar_location="below")
        for j in range(len(tool_list)):
            df1=((df['PARAMETER']==param_list[i] )& (df['ENTITY']==tool_list[j] ))
            source = ColumnDataSource(data=dict(x=x.loc[df1], y=y.loc[df1], tool=tool.loc[df1]))
            p.line(x='x', y='y',legend='tool', source=source)
            p.scatter(x='x', y='y',legend='tool',size=10,color=next(colors), source=source)
        p.add_tools(HoverTool(tooltips=[("Entity", "@tool"), ("Chart Value", "@y{%0.2f}"), ("Date", "@x{%F}")], formatters={"x": "datetime", "y": 'printf'}))
        #p.xaxis.formatter = DatetimeTickFormatter(days=["%m/%d/%Y"])
        p.legend.location = "top_right"
        p.legend.click_policy = "mute"
        row.append(p)
        i = i + 1
    grid.append(row)

fig=layout(grid)
reset_output()

show(fig)
Run Code Online (Sandbox Code Playgroud)

小智 1

您可以尝试使用 webGL 来加快速度。这允许字形通过 GPU 渲染。

figure(title=param_list[i], x_axis_label='date', y_axis_label='chart value', x_axis_type="datetime", toolbar_location="below", output_backend="webgl")
Run Code Online (Sandbox Code Playgroud)

更多信息:https://docs.bokeh.org/en/latest/docs/user_guide/webgl.html

进一步阅读有关大量绘图的 Bokeh 性能问题:https://github.com/bokeh/bokeh/issues/6294