I am trying for my app to have a callback for the click event in a plotly chart setup by streamlit. I want for the graph to not reset once the click action is performed on the chart and maintain the axis in their old positions.
Steps to reproduce
Code snippet:
import streamlit as st
from streamlit_plotly_events import plotly_events
import plotly.express as px
import pandas as pd
df = pd.DataFrame(dict(
x = [1, 3, 2, 4],
y = [1, 2, 3, 4]
))
fig = px.line(df, x="x", y="y", title="Unsorted Input", markers=True)
selected_points = plotly_events(fig, key="test")
st.write(selected_points)
This is a small reproducible example. The following libs are required:
streamlit
pandas
plotly
streamlit_plotly_events
Run this code using streamlit run main.py and then try and zoom into the chart so only one point is visible. Click on any of the (x, y) points from the chart. The chart resets to the old position (the equivalent of clicking on the reset axis button).
Expected behavior:
Load another streamlit component like a table when the point is clicked and maintain the plot in the zoomed-in position.
Actual behavior:
The graph resets to the default position and the zoomed in position is lost.
Thank you for sharing a reproducible example. This question has been asked previously and answered. Here’s the solution:
Note: the previous thread was purely using Plotly and not the streamlit_plotly_events custom component. Let us know if the attached solution doesn’t work for you.
Thanks for your reply. The only reason I am using the streamlit_plotly_events package is bring interactivity into my plots. I want to load a small section based on which points were clicked in a scatter plot. I am not sure I can do that with just streamlit yet? Is my understanding correct? I will try the solution you recommended and post back if that works for me.
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.