✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Scatter lines - Issue in Mutli Drop Down Input Color Code

Hi,

I’m new to coding, python and plotly in general. I am trying to create a Covid tracker, where in one can select multiple countries as inputs and the tracker correlates the confirmed case count for the selected countries.

I am having trouble in trying to give each selected country a different color, but at the moment I am unable to fix it.

I was able to resolve my issue with plotly.express, but still not able to solve my query using go.Scatter (and i can’t get my head over it)

Here’s a snippet my code:

country_options =
for country in alldata[‘Country’].dropna().unique():
country_options.append({‘label’:str(country),‘value’:str(country)})

app.layout = html.Div([
html.H3(
children=‘Select the Country below’,
style={
‘textAlign’: ‘left’
}
),
dcc.Dropdown(id=‘country-picker’,
options=country_options,
value=[‘India’,‘Pakistan’,‘Afghanistan’],
multi=True
),
html.Br(),
html.Button(
id=‘submit-button’,
n_clicks=0,
children=‘Submit’,
style={‘background-color’: ‘4CAF50’,
‘border’: None,
‘color’: ‘white’,
‘padding’: ‘15px 32px’,
‘text-align’: ‘center’,
‘text-decoration’: None,
‘font-size’: ‘10px’,
‘border-radius’: ‘10px’
}
),

html.H3(
    children='Corona Cases Breakdown:',
    style={
        'textAlign': 'left'
    }
),
dcc.Graph(id='country-wise-corona')

])

@app.callback(Output(‘country-wise-corona’,‘figure’),
[Input(‘submit-button’, ‘n_clicks’)],
[State(‘country-picker’, ‘value’)])
def update_figure(n_clicks,selected):

#traces = []

filtered_dataset = alldata[alldata['Country'].isin(selected)]
filtered_dataset = filtered_dataset.sort_values(by=['Date','Country'], ascending=True)   
#fig = px.line(filtered_dataset, x='Date', y='Confirmed Cases', color='Country')
#return fig

fig = go.Figure()

for val in selected:
    print (val + " printed")
    # Add traces
    fig.add_trace(go.Scatter(x=filtered_dataset['Date'],
                             y=filtered_dataset['Confirmed Cases'],
                             mode='lines',
                             text=val,
                             name=val,
                            )
                 )
    fig.update_traces(mode='markers', marker_line_width=2, marker_size=10)
    fig.update_layout(title='Confirmed Cases So Far',
                      hovermode = 'closest',
                      yaxis_zeroline=False, 
                      xaxis_zeroline=False)
    
return fig

app.run_server()

Here’s the response I am getting:

Issue Has been resolved at my end. Turns out, the filtered dataset that i was creating earlier should have been created inside the for loop.