Maintain plot in zoomed in state after on_click plotly_event


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 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")

This is a small reproducible example. The following libs are required:

  • streamlit
  • pandas
  • plotly
  • streamlit_plotly_events

Run this code using streamlit run 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.

Debug info

  • Streamlit version: 1.15.0
  • Python version: 3.7
  • Using Conda? PipEnv? PyEnv? Pex?
  • OS version: macos Ventura
  • Browser version: Chrome/Safari
1 Like

Hi @aj7777,

Welcome to the Streamlit community! :wave: :smile:

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.

Happy Streamlit-ing! :balloon:

Hi Snehan,

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.


This worked for me! Thank you for the help Snehan.

1 Like