What's the command to “reset” a bokeh plot?

前端 未结 3 439
生来不讨喜
生来不讨喜 2021-01-04 07:46

I have a bokeh figure that has a reset button in the toolbar. Basically, I want to \"reset\" the figure when I update the data that I\'m plotting in the figure. How can I do

3条回答
  •  花落未央
    2021-01-04 08:24

    Example with a radiogroup callback, that's the best way I found to reset while changing plots, just get the range of the data and set it to the range:

    from bokeh.plotting import Figure
    from bokeh.models import ColumnDataSource, CustomJS, RadioGroup
    from bokeh.layouts import gridplot
    from bokeh.resources import CDN
    from bokeh.embed import file_html
    
    x0 = range(10)
    x1 = range(100)
    y0 = [i for i in x0]
    y1 = [i*2 for i in x1][::-1]
    
    
    fig=Figure()
    
    source1=ColumnDataSource(data={"x":[],"y":[]})
    source2=ColumnDataSource(data={"x0":x0,"x1":x1,"y0":y0,"y1":y1})
    
    p = fig.line(x='x',y='y',source=source1)
    
    callback=CustomJS(args=dict(s1=source1,s2=source2,px=fig.x_range,py=fig.y_range),  code="""
        var d1 = s1.get("data");
        var d2 = s2.get("data");
        var val = cb_obj.active;
    
        d1["y"] = [];
        var y = d2["y"+val];
        var x = d2["x"+val];
    
        var min = Math.min( ...y );
        var max = Math.max( ...y );
    
        py.set("start",min);
        py.set("end",max);
    
        var min = Math.min( ...x );
        var max = Math.max( ...x );
    
        px.set("start",min);
        px.set("end",max);
    
        for(i=0;i<=y.length;i++){
            d1["y"].push(d2["y"+val][i]);
            d1["x"].push(d2["x"+val][i]);
        }
    
        s1.trigger("change");
        """)
    
    radiogroup=RadioGroup(labels=['First plot','Second plot'],active=0,callback=callback)
    
    grid = gridplot([[fig,radiogroup]])
    
    outfile=open('TEST.html','w')
    outfile.write(file_html(grid,CDN,'Reset'))
    outfile.close()
    

    The Bokeh website is seriously lacking in examples for different ways to set callbacks for the different widgets.

提交回复
热议问题