Black Lives Matter. Please consider donating to Black Girls Code today.

Selecting points in Python

Is the box/lasso selection tool in Python purely for highlighting points on the graph visually? Or is there a way to capture in the backend which points have been selected (e.g and using that to change another plot)?


Yes, use Dash for this. See the “Graph Crossfilter” section here:

Is it possible within a Jupyter notebook?

Sorry, I should have clarified.

1 Like

Dear WindChimes,
I am very new to plotly, but i discovered an attribute on my scatter plot object named selectedpoints which returns the indicies of selected points from my FigureWidget in jupyter lab.

Seems like a recent update.


Here’s an example of responding to data selection in the Jupyter Notebook using the version 3 FigureWidget

Here is a really (really) simple example of using the box select or lasso to crossfilter.

import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objs as go
from dash.dependencies import Input,Output

# Use the 'Cars' dataset

cars = pd.read_csv('')

# Build simple Dash Layout

app = dash.Dash(__name__)

app.layout = html.Div([
    html.Button(id='ignore',style={'display':'none'}), #Create a hidden button just so the first callback will have an input.  It doesn't so anything.
    dcc.Graph(id='carGraph'), #Graph that displays all data
    html.Div(id='display'),  #To show format of selectData
    dcc.Graph(id='filterGraph') #Graph that shows only filtered data    

# Create graph with data
def testfunc(clicks):
    trace1 = go.Scattergl(x=cars['disp'],y=cars['hp'],mode='markers',text=cars['model'])
    layout=go.Layout(title='All Data')
    return {'data':[trace1],'layout':layout}

# Show result of selecting data with either box select or lasso
def selectData(selectData):
    return str('Selecting points produces a nested dictionary: {}'.format(selectData))

#Extract the 'text' component and use it to filter the dataframe and then create another graph
def selectData3(selectData):
    filtList = []
    for i in range(len(selectData['points'])):

    filtCars = cars[cars['model'].isin(filtList)]
    trace2 = go.Scattergl(x=filtCars['disp'],y=filtCars['hp'],mode='markers',text=filtCars['model'])
    layout2 = go.Layout(title='Filtered Data')
    return {'data':[trace2],'layout':layout2}  

if __name__ == '__main__':

Hope it helps…