I will try to explain it clearly.
I am performing a dash application, and I want to dinamically add new traces to a scatter plot. Here is an example of the code.
app = dash.Dash(name, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.scripts.config.serve_locally = True
app.config[‘suppress_callback_exceptions’] = True
app.layout = html.Div([
html.Div([
dcc.Dropdown(id=‘scatterXVar’, value=None, style={‘display’: ‘none’}),
dcc.Dropdown(id=‘scatterYVar’, value=None, style={‘display’: ‘none’}),
], id=‘scatterVariables’),
html.Button(‘Add Trace’, id=‘addTrace’)
)]),
dcc.Graph(id=‘scatterGraph’)
I have two dropdowns in the layout, one corresponds to the X-Variable I want to scatter, and the other one to the Y-Variable.
The idea is, once I have selected which two variables I wanna scatter, whenever I click the button, a trace is added to the graph. My problem is that after I scatter the first trace, whenever I want to add another trace to the graph, the previous traces are erased, and it only adds the new trace, and what I want is to preserve all the traces together.
Here is my callback:
@app.callback(Output(‘scatterGraph’, ‘figure’),
[Input(‘addTrace’, ‘n_clicks’)],
[State(‘scatterXVar’, ‘value’),
State(‘scatterYVar’, ‘value’)])
def sccatterGr(nclicks, scatterXval, scatterYval): # da):
if scatterXval is None or scatterYval is None or scatterXval == or scatterYval == :
return {}
else:
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df[scatterXval.encode(‘utf-8’)],
y=df[scatterYval.encode(‘utf-8’)],
mode=‘markers’,
marker={
‘size’: 15,
‘opacity’: 0.5,
‘line’: {‘width’: 0.5, ‘color’: ‘white’}
}))
return fig
Please, I need help so badly.