plotly.py
plotly.py copied to clipboard
uirevision property doesn't keep the viewpoint consistent
The following is the copy-paste of a question I opened on stackoverflow, it looks like to be a bug, so here i go:
I'm trying to keep the viewpoint consistent in my plotly-dash web app after every callback. This should be straightforward according to many already answered questions. The following line of code should do the magic:
fig.update_layout(uirevision='constant')
Unfortunately for me, it looks like that this is not the case for 3D scatter plots, here is a very brief piece of code that should allow to reproduce my problem:
import plotly.express as px
df = px.data.iris()
import dash
from dash import dcc
from dash import html
from dash import Input, Output
app = dash.Dash()
app.layout = html.Div([
dcc.Dropdown(['option1', 'option2', 'option3'], 'axis', id='x-dropdown', clearable=False),
dcc.Graph(id='main-plot')
])
@app.callback(
Output('main-plot', 'figure'),
Input('x-dropdown', 'value'))
def update_figure(x_selector):
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width', color='species')
fig.update_layout(uirevision='constant')
return fig
app.run_server(debug=True, use_reloader=False)
I can tell that the uirevision
is doing something since the viewpoint is not completely reset after the callback, but it is still very imprecise. Is there something I'm missing or is this the expected behavior?
Following up on @lpuglia's question, as I'm facing the same problem. I've tried several combinations of uirevsion in layout and in scene, but with no success.
Are there any updates on this issue? Thanks
Any update on this issue? I'm also experiencing the same thing
plotly 5.17.0
Same issue for me, 3d scatter plot seems to ignore uirevision.
I'm instead having to store the state of the camera.
@DoctorDinosaur can you share a workaround?
app = Dash(__name__)
app.layout = html.Div(
[
# ... Your dash elements
dcc.Store(id="camera"),
]
)
# Callback to store camera position
@app.callback(
dd.Output("camera", "data"),
dd.Input("graph", "relayoutData"),
)
def store_camera_position(relayoutData):
if relayoutData is not None:
if "scene.camera" in relayoutData:
return relayoutData["scene.camera"]
return no_update
# ... some code ....
@app.callback(
dd.Output("graph", "figure"),
# ... Your other callback stuff
dd.State("graph", "figure"),
dd.State("camera", "data"),
)
def your_callback_func(... , figure, camera):
if camera is not None:
figure["layout"]["scene"]["camera"] = camera
# ... The rest of your callback func
return figure
Little buggy, but better than nothing. I'm sure someone with more experience can refine this.
I have an observation that might be useful to whoever that's going to fix this problem.
I have a graph object Scatter3d plot. uirevision works fine when I update the plot data. But it breaks down when I maximize the browser tab (in general, change tab size). From that point on, the camera resets each time there is an update to the plot.