批量添加图到散景
Bulk adding plots to Bokeh
我想根据每条线的两个点将 10,000 条线添加到 bokeh
图中。一个一个地添加它们非常慢,最多可能需要一个小时。有什么办法可以加快速度吗?
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)
编辑:
多行
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)
编辑:对于这种具有单段线的特定应用,最好的解决方案是使用矢量化 segment
字形方法。
对于这种用法,Bokeh 不是正确的工具,至少它本身不是。为了支持各种交互功能,Bokeh 针对更少的字形进行了显式优化,每个字形具有更多数据。每个新字形都会产生固定的开销,而 10000 个字形对于 Bokeh 本身来说永远是不合理的。一种选择可能是使用所有行的所有数据对 multi_line
进行一次调用,而不是对 line
进行数千次不同的调用。但是,您可能还想看看 Datashader,它对于可视化更大的数据集(多达数十亿个点)很有用,它与 Bokeh 无缝集成以提供此类数据集的交互性。
我想根据每条线的两个点将 10,000 条线添加到 bokeh
图中。一个一个地添加它们非常慢,最多可能需要一个小时。有什么办法可以加快速度吗?
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)
编辑:
多行
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)
编辑:对于这种具有单段线的特定应用,最好的解决方案是使用矢量化 segment
字形方法。
对于这种用法,Bokeh 不是正确的工具,至少它本身不是。为了支持各种交互功能,Bokeh 针对更少的字形进行了显式优化,每个字形具有更多数据。每个新字形都会产生固定的开销,而 10000 个字形对于 Bokeh 本身来说永远是不合理的。一种选择可能是使用所有行的所有数据对 multi_line
进行一次调用,而不是对 line
进行数千次不同的调用。但是,您可能还想看看 Datashader,它对于可视化更大的数据集(多达数十亿个点)很有用,它与 Bokeh 无缝集成以提供此类数据集的交互性。