记住流式散景图中的选定点

时间:2015-09-11 19:50:21

标签: python plot bokeh

我正把这个问题砸在墙上。我只是希望能够在流式散景散点图中进行选择(套索,box_select)点,当图更新时(例如,时间序列中的新数据)将记住这些点。

我认为这将要求我能够访问当前所选点的索引列表,但我无法弄清楚如何。这是我尝试的一个例子(稍微修改了http://bokeh.pydata.org/en/latest/docs/user_guide/server.html#streaming-data-with-the-server的例子)。请注意,当绘图更新为新流(在此示例中为混洗)数据时,将取消选择所选点。

import time
from random import shuffle
from bokeh.plotting import figure, output_server, cursession, show

# prepare output to server
output_server("remember_selected")

p = figure(plot_width=400, plot_height=400,tools="lasso_select,box_select,help")
p.scatter([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], name='ex_line')
show(p)

# create some simple animation..
# first get our figure example data source
renderer = p.select(dict(name="ex_line"))
ds = renderer[0].data_source

while True:
    # Update y data of the source object
    shuffle(ds.data["y"])

    #Can I access currently selected points? (NO!)
    print ds.selected['2d']
    print ds.selected['1d']
    print ds.selected['0d']

    # store the updated source on the server
    cursession().store_objects(ds)
    time.sleep(2.)

1 个答案:

答案 0 :(得分:0)

要获得回调,您需要设置一个完整的Bokeh应用程序。以下证明了这一点:

from bokeh.models import ColumnDataSource, Plot
from bokeh.plotting import figure
from bokeh.properties import Instance
from bokeh.server.app import bokeh_app
from bokeh.models.widgets import HBox
from bokeh.server.utils.plugins import object_page
from random import shuffle


class App(HBox):
    extra_generated_classes = [["App", "App", "HBox"]]
    jsmodel = "HBox"

    # plots
    plot = Instance(Plot)
    source = Instance(ColumnDataSource)

    @classmethod
    def create(cls):
        # create layout widgets
        obj = cls()

        # outputs
        obj.source = ColumnDataSource(data=dict(x=[1, 2, 3, 4, 5],
                                                y=[6, 7, 2, 4, 5]))
        obj.plot = figure(x_range=(0, 6), y_range=(0, 7),
                      tools="lasso_select,box_select,help", plot_width=400,
                      plot_height=400)
        obj.plot.scatter('x', 'y', source=obj.source, name='ex_line')

        # layout
        obj.children = [obj.plot]
        obj.setup_events()
        return obj

    def setup_events(self):
        super(App, self).setup_events()
        if self.source:
            self.source.on_change('selected', self, 'click')

    def click(self, cds, name, prev, new):
        print(new['1d']['indices'])
        shuffle(self.source.data['y'])


@bokeh_app.route("/bokeh/select/")
@object_page("select")
def make():
    app = App.create()
    return app