1

I'm trying to use Bokeh to build an interactive tool that allows a user to select a subset of points from a scatter plot and to subsequently label or annotate those points. Ideally, the user-provided input would update a "label" field for that sample's row in a dataframe.

The code below allows the user to select the points, but how do I make it so that they can then label those selected points from a text-input widget e.g. text = TextInput(value="default", title="Label:") , and in so doing, change the "label" field for that sample in the dataframe?

import pandas as pd
import numpy as np

from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import HoverTool
from bokeh.models.widgets import TextInput

data = pd.DataFrame()
data["x"] = np.random.randn(100)
data["y"] = np.random.randn(100)
data["label"] = "other"

x=data.x.values
y=data.y.values
label=data.label.values

output_file("toolbar.html")


source = ColumnDataSource(
data=dict(
        x=x,
        y=y,
        _class=label,
    )
)

hover = HoverTool(
    tooltips=[
        ("index", "$index"),
        ("(x,y)", "($x, $y)"),
        ("class", "@_class"),
    ]
)

p = figure(plot_width=400, plot_height=400, tools=[hover,"lasso_select","crosshair",],
       title="Mouse over the dots")

p.circle('x', 'y', size=5, source=source)

show(p)
Feynman27
  • 3,049
  • 6
  • 30
  • 39

0 Answers0