I’d like to get point selection from a Plotly contour plot in streamlit (individual points).
Is that possible?
For instance, to get the selected data on the following example
import numpy as np
import streamlit as st
from plotly.graph_objects import Contour, Figure
x = np.linspace(-5, 5, 20)
y = np.linspace(-5, 5, 20)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))
fig = Figure()
fig.add_trace(
Contour(
z=Z,
x=x,
y=y,
colorscale="Viridis",
showscale=True,
opacity=0.8,
)
)
fig.update_layout(dragmode="zoom")
selected_data = st.plotly_chart(
fig,
on_select="rerun",
selection_mode="points",
)
# — Display Results —
if selected_data and "selection" in selected_data:
points = selected_data["selection"]["points"]
if points:
st.write(f"### You selected {len(points)} points!")
st.dataframe(points)
else:
st.info("Box select or click points on the plot.")