0

I am trying to use a scatter plot in holoviews/hvplot to explore some data and then export things I select to a file... basically so I can tag it and/or show it to an expert.

I am able to make a scatter plot and a table that is linked and shows the selected points from the lasso tool. BUT I can't figure out how to only show the selected points and to then export those to either a Pandas dataframe or to anything else that I could work with.

My code is something like what follows.

points = df.hvplot.scatter(x="comp1", y="comp2", c="label", width=1000, height=1000).opts(tools=["hover", "lasso_select", "box_select"])
table = hv.Table(points, ["comp1", "comp2"], "label")
DataLink(points, table)
(table + points)

I see that a points has a select method available but it seems to show all points. What am I missing?

TIA

Matt Camp
  • 1,448
  • 3
  • 17
  • 38

1 Answers1

4

The select method allows you to apply a selection by value in Python so has nothing to do with the selection you have performed in bokeh. If you want to access the selection made using the select tool you can have a look at the Selection1D stream. Linked streams such as that one provide a mechanism to access values from Javascript in Python. In your example you could do something like this:

points = df.hvplot.scatter(x="comp1", y="comp2", c="label", width=1000, height=1000).opts(tools=["hover", "lasso_select", "box_select"])
table = hv.Table(points, ["comp1", "comp2"], "label")
DataLink(points, table)
sel = hv.streams.Selection1D(source=points)
(table + points)

Finally in a new cell you can access and save selection with:

points.iloc[sel.index].data.to_csv('selected.csv')
philippjfr
  • 3,997
  • 14
  • 15