Hello!
I would like to plot an average signal (graph below) for each separate selection in the scatterplot (top graph). The problem is that I cannot differentiate between different selections.
curveNumber
in selectedData
is always 0 in my case. Is there a way to extract this information? Thank you!
def update_scatterplot():
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df['water'],
y=df['cell'],
mode='markers',
marker=dict(size=10, opacity=0.7, color=df['lysis'].astype("category").cat.codes, colorscale='Viridis'),
# customdata=np.arange(len(df)), # Ensure customdata matches signal_array indices
name="Scatterplot"
))
fig.update_layout(
title="Scatterplot",
xaxis_title="X-axis",
yaxis_title="Y-axis",
dragmode='select'
)
return fig
# Initialize Dash app
app = Dash(__name__)
styles = {
'pre': {
'border': 'thin lightgrey solid',
'overflowX': 'scroll'
}
}
# Layout of the app
app.layout = html.Div([
dcc.Graph(
id='scatterplot',
figure=update_scatterplot(),
),
dcc.Graph(id='signal-plot'),
html.Div([
dcc.Markdown("**Selection Data**"),
html.Pre(id='selected-data', style=styles['pre']),
], className='three columns'),
])
@app.callback(
Output('selected-data', 'children'),
Input('scatterplot', 'selectedData')
)
def display_selected_data(selectedData):
return json.dumps(selectedData, indent=2)
# Callback to update signal plot based on selection
@app.callback(
Output('signal-plot', 'figure'),
Input('scatterplot', 'selectedData')
)
def update_signal_plot(selected_data):
fig = go.Figure()
print(selected_data)
if selected_data:
# Extract selected points
selected_ids = [point['pointIndex'] for point in selected_data['points']]
avg_signal = signal_array[selected_ids, :].mean(axis=0)
else:
avg_signal = signal_array.mean(axis=0)
# Create line plot for the average signal
fig.add_trace(go.Scatter(
x=wl,
y=avg_signal,
mode='lines',
name="Average Signal"
))
fig.update_layout(
title="Average Signal",
xaxis_title="Timepoint",
yaxis_title="Signal Value"
)
return fig
The selectedData
for the provided image:
{
"points": [
{
"curveNumber": 0,
"pointNumber": 19,
"pointIndex": 19,
"x": 0.23084526968953495,
"y": 0.2632524731791809,
"customdata": 19,
"marker.color": 1
},
# ...
{
"curveNumber": 0,
"pointNumber": 9983,
"pointIndex": 9983,
"x": 0.04727233565169021,
"y": 0.5013248475937009,
"customdata": 9983,
"marker.color": 0
}
],
"range": {
"x": [
0.14421608454120896,
0.2879316670537672
],
"y": [
0.15162525690270545,
0.3464139081061809
]
}
}