代码之家  ›  专栏  ›  技术社区  ›  user2242044

向Bokeh批量添加绘图

  •  0
  • user2242044  · 技术社区  · 7 年前

    我想将10000行添加到 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)
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   bigreddot    7 年前

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

    对于这种用法,Bokeh不是合适的工具,至少它本身不是。为了支持各种交互式功能,Bokeh显式优化了更少的glyph,每个glyph有更多的数据。每一个新的雕文都会带来固定的开销,而10000个雕文对于Bokeh本身永远都不合理。一种选择可能是对 multi_line 所有线路的所有数据,而不是对 line . 但是,您可能还想看看 Datashader 它对于可视化更大的数据集(多达数十亿个点)非常有用,并且与Bokeh无缝集成,以提供此类数据集上的交互。