I have a dash plot that includes two distributions and I want a vertical line to indicate the mean of the first distribution. I’d like all three pieces to be updated with a slider callback.
This plotting function successfully gets called by a callback initiated by a slider in my app. dist1, dist2, and dist1_mean all get updated, but regardless of the slider position, the vertical line stays fixed at its initial value. It’s like the add_vline isn’t getting updated with the callback even though a new fig object is presumably being created each time.
I’ve confirmed dist1_mean is changing depending on the filter and slider value.
def plot_distribution_slider(self, df, filter_ind, slider_ind): filter = (#filter_ind#) dist1 = df.loc[filter, 'dist1_cols'].iloc[slider_ind] dist2 = df.loc[filter, 'dist2_cols'].iloc[slider_ind] dist1_mean = df.loc[filter, 'dist1_mean_col'].iloc[slider_ind] x_axes = list(range(#, #)) fig = go.Figure() # Distribution 1 fig.add_trace( go.Scatter( x=x_axes, y=dist1, name='dist1' ) ) # Distribution 2 fig.add_trace( go.Scatter( x=x_axes, y=dist2, name='dist2' ) ) fig.update_layout( #### ) fig.add_vline( x=dist1_mean, ) return fig
My callback, fwiw:
@app.callback( Output('distributions', 'figure'), Input('all-data-graph', 'clickData'), Input('time-slider', 'drag_value')) def update_distribution(click, slider_ind): filter_ind = df.loc[df.index == click['points']['x'], 'runs'].values return Plot().plot_distribution_slider(df, filter_ind, slider_ind)
I’ve had this same issue in the past with add_vline and thought maybe it was just some issue with my current environment, but it’s still not working. I fixed it previously by manually adding a line shape. But this is a verbose work-around. Id’ prefer to get this seemingly very simple feature working.
Here was my previous solution, for those having the same issue:
fig_std.update_layout( ### shapes=[ dict( type= 'line', yref= 'paper', y0= 0, y1= 1, xref= 'x', x0= dist_mean, x1= dist_mean, line=dict( color="Blue", width=2, dash="dashdot", ) ), ) ])
I seem to have isolated the bug. When I fix the y-axis, the vertical line doesn’t get updated. If I then adjust the plot by zooming in etc, the vertical line gets updated. Here is the culprit:
fig.update_layout( yaxis=dict( range=[-1, 50] ), )
Regardless of whether I fix the yaxis before or after setting the vertical line, it’s failing to update.