I have a streamlit app where I’m displaying a bunch of points as a 3d scatter plot using plotly (plotly.express.scatter_3d). I have a couple of sliders that are used to as input to the function filters and colors certain points for plotting. All that works as expected.
The problem is that every time I change the sliders the view of my 3D plot resets. I want to be able to first rotate/zoom my 3d plot to get the optimal view of my data and then change some arguments with my sliders to see how my data changes. Is there any way to achieve this?
I have encountered a similar situation. The issue is that when streamlit reruns the python code, the Plotly 3D viewer somehow ends up in a different state. Here is a simple example:
import streamlit as st
from pandas import DataFrame
from numpy.random import random
import plotly.graph_objects as go
data1 = DataFrame(random((100, 3)))
data2 = DataFrame(random((100, 3)))
trace1 = [go.Scatter3d(x=data1[0], y=data1[1], z=data1[2])]
trace2 = [go.Scatter3d(x=data2[0], y=data2[1], z=data2[2])]
fig = go.Figure(trace1 + trace2, layout=go.Layout())
st.plotly_chart(fig, use_container_width=True)
To reproduce:
copy the above code into test.py
Bring up the webpage with streamlit run test.py
interactively rotate the 3D view
toggle one of the traces in the legend
observation: the 3D view doesn’t change when the traces are toggled
now re-run the code by clicking “rerun” in the menu on the right
toggle one of the traces in the legend
observation: the 3D view now snaps back to the initial view when toggling traces
The same happens when any control elements like sliders or text input are used since the code is rerun each time their value changes.
Reloading the page leads back to the original state where toggling traces doesn’t change the 3D view, but this obviously doesn’t work when any input elements are present.
This issue is somehow critical to the app I’m working on, but I haven’t found any way to prevent Plotly for behaving like this yet. Not sure if this should be viewed as a Plotly bug or Streamlit bug, but if it is the former it would illustrate a situation where Streamlit’s basic design idea (rerunning everything on input change and caching computationally expensive parts) does not work.
I really hope this can be resolved, because otherwise Streamlit is just so cool
If you use plotly.graph_objects, you can set a uirevision=‘foo’ as part of go.Layout()(doesn’t matter what the variable is set to). Then as long as the uirevision hold the same value each time you change the slider, the view of your 3d plot should stay the same.
import plotly.graph_objs as go
figure = {"data": [go.Surface(z=z,
y=y,
x=x)],
"layout": go.Layout(height=700,
margin=dict(l=0, r=0, b=0, t=30),
uirevision='foo')}
st.plotly_chart(figure)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.