For those interested, here is a simple dash example:
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
df = pd.DataFrame([['A', 1, 7], ['A', 2, 6], ['A', 3, 14], ['B', 1, 3], ['B', 2, 9], ['B', 3, 2], ['C', 1, 1], ['C', 2, 5], ['C', 3, 11]], columns = ['ID', 'x_val', 'y_val'])
app = dash.Dash('Plotly dynamic traces')
app.layout = html.Div([
dcc.Dropdown(
id='my-dropdown',
options=[
{'label': i, 'value': i} for i in df.ID.unique()
], multi=True, placeholder='Select id...',
value=[df.ID.min()]
),
dcc.Graph(id='my-graph')
], style={'width': '500'})
@app.callback(Output('my-graph', 'figure'), [Input('my-dropdown', 'value')])
def update_graph(selected_dropdown_value):
grouped_df = df[df['ID'].isin(selected_dropdown_value)].groupby(by=['ID'])
layout = dict(
xaxis = dict(title="X-title"), yaxis=dict(title="Y-title")
)
data = []
for ID, group in grouped_df:
trace = {
"type": 'scatter',
"x": group['x_val'],
"y": group['y_val'],
"name": ID,
}
data.append(trace)
return {"data": data, "layout": layout}
if __name__ == '__main__':
app.run_server()