Get point selection from Plotly contour plot

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

Hey there, thanks for your question! :balloon: Currently, Streamlit’s point selection with st.plotly_chart only works for certain Plotly chart types (like scatter, line, and bar), but not for contour plots. When you try to select points on a contour plot, the selection data will remain empty—this is a known limitation and not a bug in your code. See the discussion and examples in the docs and community forums for confirmation: according to the Streamlit Plotly chart docs and related GitHub issues, only supported chart types will return selection data.

If you need interactive selection on a contour plot, you may need to use a scatter plot overlay or another supported chart type. For more advanced or custom interactivity, consider using a third-party component or a different visualization approach.

Sources:

I’ve already tried the scatter plot overlaid approach and it works fine for selection.

The problem is: when making the scatter plot over the contour plot, the hover information on the contour plot is not visible anymore (x, y, z values).