# Lasso deselect from inside a function

**URL:** <https://community.plotly.com/t/lasso-deselect-from-inside-a-function/19112>\
**Category:** 📊 Plotly Python\
**Created:** [February 6, 2019, 11:49am UTC](https://community.plotly.com/t/lasso-deselect-from-inside-a-function/19112 "2019-02-06T11:49:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexboiboi](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/alexboiboi/32/6420_2.png) [@Alexboiboi](https://community.plotly.com/u/Alexboiboi)\
**Post date:** [February 6, 2019, 11:49am UTC](https://community.plotly.com/t/lasso-deselect-from-inside-a-function/19112/1 "2019-02-06T11:49:40Z")

</div>

Hi there,

I am trying to use the lasso to select data on one plot and show in in the other plot via indices and vice versa, however I cannot make it work since the data of the first plot stays selected. Does anyone know how to call a lasso (or range) deselect from a function?

here an attemp to make it work:

```python

import numpy as np
import plotly.graph_objs as go
import ipywidgets as widgets
from plotly.colors import DEFAULT_PLOTLY_COLORS

x = np.linspace(0,6,100)
y = np.sin(x)

data = [go.Scattergl(x=x, y=y, mode='markers', name = 'All nodes', selected=dict(marker=dict(color=DEFAULT_PLOTLY_COLORS[1]))),
        go.Scattergl(mode='markers', name='selected nodes')]

layout = dict(title=dict(text = 'plot1'), dragmode='lasso')
fig = go.FigureWidget(data=data, layout= layout)

data2 = [go.Scattergl(x=y, y=x, mode='markers', name = 'All nodes', selected=dict(marker=dict(color=DEFAULT_PLOTLY_COLORS[1]))),
        go.Scattergl(mode='markers', name='selected nodes')]

layout2 = dict(title=dict(text = 'plot2'), dragmode='lasso')
fig2 = go.FigureWidget(data=data2, layout= layout2)

def selection_fn(trace, points, selector):
    # !!!! here call deselect lasso from plot2
    fig.data[1].x = [None]
    fig.data[1].y = [None]
    fig.layout.xaxis.autorange = True
    fig2.data[1].x = y[points.point_inds]
    fig2.data[1].y = x[points.point_inds]
    
    
def selection_fn2(trace, points, selector):
    # !!!! here call deselect lasso from plot1
    fig2.data[1].x = [None]
    fig2.data[1].y = [None]
    fig.data[1].x = x[points.point_inds]
    fig.data[1].y = y[points.point_inds]

fig.data[0].on_selection(selection_fn)
fig2.data[0].on_selection(selection_fn2)
display(widgets.HBox([fig,fig2]))

```

I made this as an alternative but it was not really what i aimed to do:

```python
import numpy as np
import plotly.graph_objs as go
import ipywidgets as widgets
from plotly.colors import DEFAULT_PLOTLY_COLORS

x = np.linspace(0,6,100)
y = np.sin(x)

data = [go.Scattergl(x=x, y=y, mode='markers', name = 'All nodes', marker=dict(color=np.array([DEFAULT_PLOTLY_COLORS[0]]*len(x))),
                     selected=dict(marker=dict(color=DEFAULT_PLOTLY_COLORS[1])))]

layout = dict(title=dict(text = 'plot1'), dragmode='lasso')
fig = go.FigureWidget(data=data, layout= layout)

data2 = [go.Scattergl(x=y, y=x, mode='markers', name = 'All nodes', marker=dict(color=np.array([DEFAULT_PLOTLY_COLORS[0]]*len(x))),
                      selected=dict(marker=dict(color=DEFAULT_PLOTLY_COLORS[2])))]

layout2 = dict(title=dict(text = 'plot2'), dragmode='lasso')
fig2 = go.FigureWidget(data=data2, layout= layout2)

def selection_fn(trace, points, selector):
    col = np.array([DEFAULT_PLOTLY_COLORS[0]]*len(x))
    col[points.point_inds] = np.array([DEFAULT_PLOTLY_COLORS[1]]*len(points.point_inds))
    fig2.data[0].marker.color = col
    
def selection_fn2(trace, points, selector):
    col = np.array([DEFAULT_PLOTLY_COLORS[0]]*len(x))
    col[points.point_inds] = np.array([DEFAULT_PLOTLY_COLORS[2]]*len(points.point_inds))
    fig.data[0].marker.color = col

fig.data[0].on_selection(selection_fn)
fig2.data[0].on_selection(selection_fn2)
display(widgets.HBox([fig,fig2]))

```

---

<div class="post-metadata">

**Author:** ![jmmease](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jmmease/32/3706_2.png) [@jmmease](https://community.plotly.com/u/jmmease)\
**Post date:** [February 7, 2019, 10:43am UTC](https://community.plotly.com/t/lasso-deselect-from-inside-a-function/19112/2 "2019-02-07T10:43:11Z")

</div>

Hi @Alexboiboi,

You can use the `scatter.selectedpoints` property to control which points are selected programmatically. Here’s an example:

```python
import numpy as np
import plotly.graph_objs as go
import ipywidgets as widgets
from plotly.colors import DEFAULT_PLOTLY_COLORS

x = np.linspace(0,6,100)
y = np.sin(x)

data = [go.Scattergl(x=x, y=y, mode='markers', name = 'All nodes',
                     selected=dict(marker=dict(color=DEFAULT_PLOTLY_COLORS[1])))]

layout = dict(title=dict(text = 'plot1'), dragmode='lasso')
fig = go.FigureWidget(data=data, layout= layout)

data2 = [go.Scattergl(x=y, y=x, mode='markers', name = 'All nodes',
                      selected=dict(marker=dict(color=DEFAULT_PLOTLY_COLORS[1])))]

layout2 = dict(title=dict(text = 'plot2'), dragmode='lasso')
fig2 = go.FigureWidget(data=data2, layout= layout2)

def selection_fn(trace, points, selector):
    fig.data[0].selectedpoints = None
    fig.data[0].selectedpoints = []
    fig2.data[0].selectedpoints = points.point_inds 

    
def selection_fn2(trace, points, selector):
    fig2.data[0].selectedpoints = None    
    fig2.data[0].selectedpoints = []
    fig.data[0].selectedpoints = points.point_inds

fig.data[0].on_selection(selection_fn)
fig2.data[0].on_selection(selection_fn2)
display(widgets.HBox([fig,fig2]))

```

 ![brushing](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/a/a4fcd93f3c363988aeee32ca846a5fbd4ede7675.gif)

Does this do what you want? Note that I set the `selectedpoints` property to `None` and then to `[]`. This was to work around a problem I was seeing where the `selectedpoints` update would only be processed the first time the callback was called. I think it has something to do with the order in which the `selectedpoints` property is updated and the when the `on_selection` callback is called.

Hope that helps!

-Jon

---

<div class="post-metadata">

**Author:** ![Alexboiboi](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/alexboiboi/32/6420_2.png) [@Alexboiboi](https://community.plotly.com/u/Alexboiboi)\
**Post date:** [February 11, 2019, 4:31pm UTC](https://community.plotly.com/t/lasso-deselect-from-inside-a-function/19112/3 "2019-02-11T16:31:38Z")

</div>

Hi Jon, thanks a lot for your answer, this is exactly what I needed. You are the best!

I was also wondering why the selectedpoints update was only performed only once!

---

<div class="post-metadata">

**Author:** ![jmmease](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jmmease/32/3706_2.png) [@jmmease](https://community.plotly.com/u/jmmease)\
**Post date:** [February 12, 2019, 10:47am UTC](https://community.plotly.com/t/lasso-deselect-from-inside-a-function/19112/4 "2019-02-12T10:47:18Z")

</div>

Great!

I also opened an issue about the `selectedpoints` issue at [https://github.com/plotly/plotly.py/issues/1433](https://github.com/plotly/plotly.py/issues/1433). So hopefully we can get rid of the need to perform the `None` assignment.

-Jon

---

<div class="post-metadata">

**Author:** ![visual](https://avatars.discourse-cdn.com/v4/letter/v/ee7513/32.png) [@visual](https://community.plotly.com/u/visual)\
**Post date:** [February 21, 2019, 1:48pm UTC](https://community.plotly.com/t/lasso-deselect-from-inside-a-function/19112/5 "2019-02-21T13:48:36Z")

</div>

Hi, Could I ask you how to display two diagrams in one html file as in your diagram?

---

<div class="post-metadata">

**Author:** ![jmmease](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jmmease/32/3706_2.png) [@jmmease](https://community.plotly.com/u/jmmease)\
**Post date:** [February 22, 2019, 10:47am UTC](https://community.plotly.com/t/lasso-deselect-from-inside-a-function/19112/6 "2019-02-22T10:47:31Z")

</div>

Hi @visual,

The easiest thing to do here is to use subplots: [https://plot.ly/python/subplots/](https://plot.ly/python/subplots/), and then save the figure to HTML with `plotly.offline.plot` If that doesn’t work for you, you could use an approach like I describe in [How to make Html Report readable from all browsers](https://community.plotly.com/t/how-to-make-html-report-readable-from-all-browsers/14320/10?u=jmmease) to generate a custom HTML file with multiple figures.

Hope that helps!  
-Jon

---

<div class="post-metadata">

**Author:** ![visual](https://avatars.discourse-cdn.com/v4/letter/v/ee7513/32.png) [@visual](https://community.plotly.com/u/visual)\
**Post date:** [February 24, 2019, 6:39am UTC](https://community.plotly.com/t/lasso-deselect-from-inside-a-function/19112/7 "2019-02-24T06:39:31Z")

</div>

Hi Jon, thanks a lot for your answer! I am trying to make the above interactive selection, but it seemed to have failed. This is my code.

from IPython.display import display,HTML  
from plotly.offline import plot  
import numpy as np  
import plotly.graph\_objs as go  
import ipywidgets as widgets  
from plotly.colors import DEFAULT\_PLOTLY\_COLORS

x = np.linspace(0,6,100)  
y = np.sin(x)

data = [go.Scattergl(x=x, y=y, mode=‘markers’, name = ‘All nodes’,  
 selected=dict(marker=dict(color=DEFAULT\_PLOTLY\_COLORS[1])))]

layout = dict(title=dict(text = ‘plot1’), dragmode=‘select’)  
fig1 = go.FigureWidget(data=data, layout= layout)

data2 = [go.Scattergl(x=y, y=x, mode=‘markers’, name = ‘All nodes’,  
 selected=dict(marker=dict(color=DEFAULT\_PLOTLY\_COLORS[1])))]

layout2 = dict(title=dict(text = ‘plot2’), dragmode=‘select’)  
fig2 = go.FigureWidget(data=data2, layout= layout2)

def selection\_fn(trace, points, selector):  
 fig1.data[0].selectedpoints = None  
 fig1.data[0].selectedpoints = []  
 fig2.data[0].selectedpoints = points.point\_inds

def selection\_fn2(trace, points, selector):  
 fig2.data[0].selectedpoints = None  
 fig2.data[0].selectedpoints = []  
 fig1.data[0].selectedpoints = points.point\_inds

fig1.data[0].on\_selection(selection\_fn)  
fig2.data[0].on\_selection(selection\_fn2)  
display(widgets.HBox([fig1,fig2]))

div1 = plot(fig1, output\_type=‘div’, include\_plotlyjs=True)  
div2 = plot(fig2, output\_type=‘div’, include\_plotlyjs=True)

html = “”"  
\<html\>  
 \<head\>  
 \<script src=“[https://cdn.plot.ly/plotly-latest.min.js](https://cdn.plot.ly/plotly-latest.min.js)”\>\</script\>  
 \</head\>  
 \<body\>  
 {div1}  
 {div2}  
 \</body\>  
\</html\>  
“”".format(div1=div1, div2=div2)

with open(‘multi\_plot.html’, ‘w’) as f:  
 f.write(html)

---

<div class="post-metadata">

**Author:** ![jmmease](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jmmease/32/3706_2.png) [@jmmease](https://community.plotly.com/u/jmmease)\
**Post date:** [February 25, 2019, 11:10am UTC](https://community.plotly.com/t/lasso-deselect-from-inside-a-function/19112/8 "2019-02-25T11:10:43Z")

</div>

Hi @visual,

I didn’t realize you were intending to display two diagrams with the lasso selection callbacks in an HTML file. You won’t be able to use the FigureWidget callbacks to accomplish this because these rely on having an active Python kernel.

What you want is possible, but it’s would be a bit messy and would involve writing the callbacks in JavaScript. Here’s an example that shows the general idea: See this example for the general idea [Hyperlink to markers on map](https://community.plotly.com/t/hyperlink-to-markers-on-map/17858/6?u=jmmease).

Hope that helps clear things up,

-Jon

---

<div class="post-metadata">

**Author:** ![Player51](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@Player51](https://community.plotly.com/u/Player51)\
**Post date:** [May 5, 2022, 5:06pm UTC](https://community.plotly.com/t/lasso-deselect-from-inside-a-function/19112/9 "2022-05-05T17:06:52Z")

</div>

Hi @jmmease ,  
I am selecting points with lasso on my data subsample, but then I want to re-apply `selector` in `selection_fn` to my all data. Is it possible?

```auto
py.init_notebook_mode()

y = 'column1'
x = 'column2'
subsample = 5000

f = go.FigureWidget([go.Scatter(y = df[y][:subsample], x = df[x][:subsample], mode = 'markers')])
scatter = f.data[0]
N = len(df)

def update_axes(xaxis, yaxis):
    scatter = f.data[0]
    scatter.x = df[xaxis][:subsample]
    scatter.y = df[yaxis][:subsample]
    with f.batch_update():
        f.layout.xaxis.title = xaxis
        f.layout.yaxis.title = yaxis

axis_dropdowns = interactive(update_axes, yaxis = df.select_dtypes('int64').columns, xaxis = df.select_dtypes('int64').columns)

def selection_fn(trace,points,selector):
    result = pd.Series(False, index=df.index)
    result.loc[points.point_inds] = True
    result.to_csv('result.csv')

scatter.on_selection(selection_fn)

VBox((HBox(axis_dropdowns.children),f))

```
