Hi,
I have been developing a python dash interface. One part is a dropdown triggering the drawing of curves on a figure with go. Scatter() Sometimes, it makes the same curve 2 times horizontally shifted.
Sometimes when I change the dropdown value, the values sometimes overlap the older ones.
I monitor with logging what is going on and the data reported by the logger show that the figure has only a single set of data.
Also one of the strange thing is that all the modes are 'lines+markers'
but some parts of the graph become lines only…
A minimum working example is quite tricky without data but that’s how it looks like.
def init_farm_plot():
'''
Initialise the curve plot for individual farms
'''
fig_p=go.Figure()
fig_p.add_trace(go.Scatter(
x=[],
y=[],
mode='lines+markers',
name='Biomass',
yaxis='y1',
))
fig_p.add_trace(go.Scatter(
x=[],
y=[],
mode='lines+markers',
name='lice count',
yaxis='y2',
))
# trying to add empty trace for legend (didn't work)
fig_p.add_trace(go.Scatter(x=[],y=[],mode='lines',line=dict(color='#f89406', dash='dash'),name='modelled lice infestation',
yaxis='y2'))
fig_p.add_trace(go.Scatter(x=[],y=[],mode='lines',line=dict(color='firebrick', dash='dash'),name='Average lice infestation',
yaxis='y2'))
fig_p.add_shape(type='line', xref='paper',
x0=0, y0=0.5, x1=1, y1=0.5,
line=dict(color='#f89406', dash='dash'),
yref='y2'
)
for y in range(2003,2022):
fig_p.add_vline(x=datetime(year=y, month=5, day=1), line=dict(color='green', dash='dash'))
fig_p.update_layout(
yaxis= dict(title='Recorded fish farmbiomass (tons)',
showgrid=False ),
yaxis2=dict(title='Reported average lice/fish',
overlaying='y',
side='right',
showgrid=False ),
margin=dict(b=15, l=15, r=5, t=5),
template=template
)
logger.debug(fig_p)
return fig_p
def mk_farm_evo(fig_p, name, times):
'''
Plot individual farm biomass
'''
fig_p['data'][0]= go.Scatter(x= times,
y=farm_data[name]['biomasses'],
mode='lines+markers',
name='Biomass',
yaxis='y1')
fig_p['data'][1]=go.Scatter(x=lice_data.time.values,
y=farm_data[name]['lice data'],
mode='lines+markers',
name='lice count',
yaxis='y2',)
dcc.Graph(
id='progress-curves',
figure=init_farm_plot()
),
@app.callback([
Output('progress-curves','figure'),
Output('farm_layout','children'),
],
Input( 'dropdown_farms', 'value',),
State(ThemeSwitchAIO.ids.switch("theme"), "value"),
State('progress-curves','figure'),
log= True
)
def farm_inspector(name, toggle, fig_p, dash_logger: DashLogger):
template = template_theme1 if toggle else template_theme2
logger.debug(f'curve name: {name}')
if not name:
dash_logger.warning('No farm selected', autoClose=autocl)
raise PreventUpdate
else:
dash_logger.info(f'Computing curve for {name}', autoClose=autocl)
curves=mk_farm_evo(fig_p,name, times)
curves['layout']['template']=mk_template(template)
logger.debug(curves)
return curves, mk_farm_layout(name, marks_biomass,marks_lice)
if __name__ == '__main__':
app.run_server(host='0.0.0.0', port=8050, debug=True)
I am using dash 2.7.1 and plotly 5.11