If I disable a trace in the legend, I want another trace to be disabled as well. Can't find the solution

Hi all,

I am currently trying to visualize several normal values of physical fitness with corresponding confidence interval.
In this example I will only provide three for readibility.

With this problem I have two questions:

  1. I want the trace to be “linked” two the confidence interval, so when i disable one trace via the legend, I want to disable the corresponding confidence interval

  2. I want to start with two active, and one disabled trace, I THINK i got it covered by putting both trace and confidence interval trace on visible ‘legendonly’ while the confidence interval is set on showlegend=false. But I am not sure while I am not able to solve problem question 1.

I know that it has something to do with:

@app.callback

But somehow, I am not getting any further

my code until now:

import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import numpy as np
import plotly.graph_objs as go


app = dash.Dash('example')


# TAKKEN
x_takken = [ i for i in range(6,66) ]

y_takkenll=0.0002 *np.power(x_takken,3)-0.0227*np.power(x_takken,2)+0.5809*np.asarray(x_takken)+31.909
y_takken  =-0.0049*np.power(x_takken,2)+0.0884*np.asarray(x_takken)+48.263
y_takkenul=0.00005*np.power(x_takken,3)-0.0065*np.power(x_takken,2)+0.0655*np.asarray(x_takken)+64.801
x_takken_rev = x_takken[::-1]
y_takken_rev = y_takkenll[::-1]
# PREVENTION FIRST
x_PF  = [ i for i in range(21,84) ]
y_PFll= 30.1000000+0.0100000*np.asarray(x_PF)-0.0030000*np.power(x_PF,2);
y_PF  =   41.0371622+0.1378218*np.asarray(x_PF)-0.0050032*np.power(x_PF,2);
y_PFul= 53.5906250+0.1145833*np.asarray(x_PF)-0.0052083*np.power(x_PF,2);
x_PF_rev = x_PF[::-1]
y_PF_rev = y_PFll[::-1]
# JONES
x_jones=[ i for i in range(15,72) ] 
y_jonesll=(55-0.44*np.asarray(x_jones))-1.96*6.5;
y_jones=55-0.44*np.asarray(x_jones);
y_jonesul=(55-0.44*np.asarray(x_jones))+1.96*6.5
x_jones_rev = x_jones[::-1]
y_jones_rev = y_jonesll[::-1]


xaxis = go.layout.XAxis(title="Age [years]")
yaxis = go.layout.YAxis(title="VO<sub>2</sub>/kg [ml/min/kg]")

fig = go.Figure(layout=go.Layout(title="VO<sub>2</sub>max/kg as a function of age", xaxis=xaxis, yaxis=yaxis))


# TAKKEN
fig.add_trace(go.Scatter(
    x=x_takken+x_takken_rev,
    y=np.array(y_takkenul).tolist()+np.array(y_takken_rev).tolist(),
    fill='toself',
    fillcolor='rgba(0,100,80,0.2)',
    line_color='rgba(255,255,255,0)',
    showlegend=False,
    name='Takken 3<sup>th</sup> & 97<sup>th</sup> Percentile',
))

# PREVENTION FIRST
fig.add_trace(go.Scatter(
    x=x_PF+x_PF_rev,
    y=np.array(y_PFul).tolist()+np.array(y_PF_rev).tolist(),
    fill='toself',
    fillcolor='rgba(0,100,80,0.2)',
    line_color='rgba(255,255,255,0)',
    showlegend=False,
    name='Prevention first 5<sup>th</sup> & 95<sup>th</sup> Percentile',
))

# JONES
fig.add_trace(go.Scatter(
    x=x_jones+x_jones_rev,
    y=np.array(y_jonesul).tolist()+np.array(y_jones_rev).tolist(),
    fill='toself',
    fillcolor='rgba(200,100,80,0.2)',
    line_color='rgba(255,255,255,0)',
    showlegend=False,
    visible="legendonly",
    name='Jones 5<sup>th</sup> & 95<sup>th</sup> Percentile',
))

fig.add_trace(go.Scatter(x=x_PF, y=y_PF,name='Prevention First',fill='none'))
fig.add_trace(go.Scatter(x=x_takken, y=y_takken,name='Takken',fill='none'))
fig.add_trace(go.Scatter(x=x_jones, y=y_jones,name='Jones',fill='none',visible = 'legendonly'))

app.layout = html.Div([
    dcc.Graph(id='test_graph',
              figure=fig)
    ])



if __name__ == '__main__':
    app.run_server(port=8000)
1 Like