I’m trying to use the lasso selection tool to remove points from a scatter plot. Because the scatter plot has multiple traces and subplots belonging to multiple dataframes, I can’t find a way to map the limited selection properties (curveNumber, pointNumber, pointIndex) to the underlying data.
From the documentation, I was planning on using the “customdata” property to add my own index that would determine which data points to remove. However, when I try running the code below from the documentation and select a point, I see the following (notice the absence of “customdata”).
Output from plotly 6.1.2, dash 3.0.3:
{
"points": [
{
"curveNumber": 0,
"pointNumber": 1,
"pointIndex": 1,
"x": 2,
"y": 2
}
],
Expected output (from documentation):
{
"points": [
{
"curveNumber": 0,
"pointNumber": 1,
"pointIndex": 1,
"x": 2,
"y": 2,
"customdata": [
2
]
}
],
Example code:
from dash import Dash, dcc, html, Input, Output, callback
import plotly.express as px
import json
import pandas as pd
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = Dash(__name__, external_stylesheets=external_stylesheets)
styles = {
'pre': {
'border': 'thin lightgrey solid',
'overflowX': 'scroll'
}
}
df = pd.DataFrame({
"x": [1,2,1,2],
"y": [1,2,3,4],
"customdata": [1,2,3,4],
"fruit": ["apple", "apple", "orange", "orange"]
})
fig = px.scatter(df, x="x", y="y", color="fruit", custom_data=["customdata"])
fig.update_layout(clickmode='event+select')
fig.update_traces(marker_size=20)
app.layout = html.Div([
dcc.Graph(
id='basic-interactions',
figure=fig
),
html.Div(className='row', children=[
html.Div([
dcc.Markdown("""
**Hover Data**
Mouse over values in the graph.
"""),
html.Pre(id='hover-data', style=styles['pre'])
], className='three columns'),
html.Div([
dcc.Markdown("""
**Click Data**
Click on points in the graph.
"""),
html.Pre(id='click-data', style=styles['pre']),
], className='three columns'),
html.Div([
dcc.Markdown("""
**Selection Data**
Choose the lasso or rectangle tool in the graph's menu
bar and then select points in the graph.
Note that if `layout.clickmode = 'event+select'`, selection data also
accumulates (or un-accumulates) selected data if you hold down the shift
button while clicking.
"""),
html.Pre(id='selected-data', style=styles['pre']),
], className='three columns'),
html.Div([
dcc.Markdown("""
**Zoom and Relayout Data**
Click and drag on the graph to zoom or click on the zoom
buttons in the graph's menu bar.
"""),
html.Pre(id='relayout-data', style=styles['pre']),
], className='three columns')
])
])
@callback(
Output('hover-data', 'children'),
Input('basic-interactions', 'hoverData'))
def display_hover_data(hoverData):
return json.dumps(hoverData, indent=2)
@callback(
Output('click-data', 'children'),
Input('basic-interactions', 'clickData'))
def display_click_data(clickData):
return json.dumps(clickData, indent=2)
@callback(
Output('selected-data', 'children'),
Input('basic-interactions', 'selectedData'))
def display_selected_data(selectedData):
return json.dumps(selectedData, indent=2)
@callback(
Output('relayout-data', 'children'),
Input('basic-interactions', 'relayoutData'))
def display_relayout_data(relayoutData):
return json.dumps(relayoutData, indent=2)
if __name__ == '__main__':
app.run(debug=True)