Hi! I’m trying to update the range of a graph’s x-axis but having some trouble understanding why the following code doesn’t work:
n = 1000
x = np.linspace(0, n, n)
app.layout = html.Div([
html.Button('Button', id='button'),
dcc.Graph(id='graph', figure=px.line(x=x, y=x ** 2)),
])
@app.callback(
Output('graph', 'figure'),
Input('button', 'n_clicks'),
State('graph', 'figure')
)
def update(button, figure):
if button is None:
raise PreventUpdate
f = go.Figure(figure)
f.update_xaxes({'range': (50, 100)})
return f
Interestingly, the below code does work:
app.layout = html.Div([
html.Button('Button', id='button'),
dcc.Graph(id='graph', figure=px.line(x=x, y=x ** 2)),
])
@app.callback(
Output('graph', 'figure'),
Input('button', 'n_clicks'),
State('graph', 'figure')
)
def update(button, figure):
if button is None:
raise PreventUpdate
figure['layout']['xaxis'] = {'range': (50, 100)}
f = go.Figure(figure)
return f
So what am I missing? And is there a better way to achieve this?
Thank you!