Dash Reset Axes range not updating if ranges specified in Layout

I know this is an old problem, but it just bit me and i couldnt find a proper solution.
Anyways after trying some stuff i found a decent solution myself:

Instead of updating the figure, or dcc.Graph i now update the entire html.div.

I have taken your example and fixed it to show it, see below.

I have also done some testing:
When i add a random number to the id it works aswell. So i am guessing the callback somehow remembers the graph id, so when you press reset axes it looks back to the first time it generated that graph instead of the last time it was updated via a callback. When you update the div that contains the graph this somehow breaks.


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

app = dash.Dash(name)
server = app.server
app.title = ‘resetScale2d Test’

_config = {‘modeBarButtons’: [[‘resetScale2d’]]}

def make_fig(plot_type=‘linear’, x=range(21), y=range(1, 211, 10)):
xvals = list(x)
yvals = list(y)
xrange = [0, 50]
yrange = [1, 500]
if plot_type == ‘log’:
yrange = [math.log10(yr) for yr in yrange]
print(plot_type, xrange, yrange)
traces = [go.Scatter(x=xvals, y=yvals, marker={‘size’: 8}, name=‘Tens’)]

layout = go.Layout(
    xaxis=dict(range=xrange),
    yaxis=dict(type=plot_type,
               range=yrange),
    # uirevision=plot_type
)
fig = go.Figure(data=traces, layout=layout)


fig_div = html.Div(
        [dcc.Graph(id='linlogplot', figure=fig, config=_config)],
        id='fig_div',
    )

return fig_div

app.layout = html.Div(
[
dcc.Dropdown(
id=‘linlog’,
options=[
{‘label’: ‘Linear Plot’, ‘value’: ‘linear’},
{‘label’: ‘Log Plot’, ‘value’: ‘log’},
],
value=‘linear’,
),
make_fig(),
]
)

@app.callback(Output(‘fig_div’, ‘children’), [Input(‘linlog’, ‘value’)])
def change_type(plot_type):
return make_fig(plot_type=plot_type)

if name == ‘main’:
app.run_server()

2 Likes