批量添加图表到散景

时间:2017-12-26 04:30:39

标签: python python-2.7 bokeh

我想基于每一行的两个点向bokeh图添加10,000行。逐个添加它们非常慢,可能需要长达一个小时。有什么方法可以加快速度吗?

import pandas as pd
import numpy as np
from bokeh.plotting import figure, show, output_file
output_file('temp.html')

p = figure(plot_width=500, plot_height=400)
df = pd.DataFrame(np.random.randint(0,100,size=(10000, 4)), columns=['x1', 'x2', 'y1', 'y2'])
print df
for index, row in df.iterrows():
    p.line([row['x1'], row['x2']], [row['y1'], row['y2']], line_width=2)

show(p)

编辑:

with Multiline

import pandas as pd
from bokeh.models.glyphs import MultiLine
from bokeh.models import ColumnDataSource
import numpy as np
from bokeh.plotting import figure, show, output_file

output_file('temp.html')

p = figure(plot_width=500, plot_height=400,
           )
df = pd.DataFrame(np.random.randint(0, 100, size=(10000, 4)), columns=['x1', 'x2', 'y1', 'y2'])
source = ColumnDataSource(dict(
    xs=df[['x1', 'x2']].as_matrix(),
    ys=df[['y1', 'y2']].as_matrix(),
)
)

glyph = MultiLine(xs="xs", ys="ys", line_color="#8073ac", line_width=2)
p.add_glyph(source, glyph)
show(p)

1 个答案:

答案 0 :(得分:2)

编辑:对于具有单段线的特定应用,最佳解决方案是使用矢量化segment字形方法。

对于这种用法,散景不是正确的工具,至少不是它本身。为了支持各种交互功能,Bokeh明确优化了更少的字形,每个字形有更多的数据。每个新的字形都会产生固定的开销,而Bokeh本身就不会有10000个字形。一种选择可能是使用所有行的所有数据对multi_line进行一次调用,而不是对line进行数千次不同的调用。但是,您可能还需要查看Datashader,这对于可视化更大的数据集(高达数十亿个点)非常有用,并且可以与Bokeh无缝集成,以提供对这些数据集的交互性。