Keep selected labels of plot while interacting

I’m using Streamlit to show plots with multiple lines inside. I can click the labels to choose which line I want to appear/disappear. So far so good. But I have 2 problems:

First, I’ve used the code from the example in the documentation to be able to have my plot in 2 tabs: in the first, it’s a line plot, and in the second it’s a scatter plot. However, if I select some labels in the line plot, and then I would like to see how those lines appear in the scatter plot, I have to click them again as it’s not the same graph.

The second problem which is kinda the same, I have a dropdown menu to change the data for the Y-axis of my graph. But each time I change it, same problem, I have to select the labels that I want again.

So how could I “keep in memory” the selected labels of my plot, so I can keep only them even if I interact with the app’ ?

Code snippet from documentation below, you can see that if you deselect “Asia” for example from a graph, when you switch tabs you have to do it again, I would like that to be auto.

Code snippet:

import plotly.express as px
import streamlit as st

df = px.data.gapminder()

fig = px.scatter(
    df.query("year==2007"),
    x="gdpPercap",
    y="lifeExp",
    size="pop",
    color="continent",
    hover_name="country",
    log_x=True,
    size_max=60,
)

tab1, tab2 = st.tabs(["Streamlit theme (default)", "Plotly native theme"])
with tab1:
    # Use the Streamlit theme.
    # This is the default. So you can also omit the theme argument.
    st.plotly_chart(fig, theme="streamlit", use_container_width=True)
with tab2:
    # Use the native Plotly theme.
    st.plotly_chart(fig, theme=None, use_container_width=True)

I guess there is a way to do this?

Hi @Tritize

It seems you’re looking for linked interactivity of the plots.

Here are some examples on how to implement using Altair, which you can repurpose for your app.

https://altair-viz.github.io/gallery/scatter_with_layered_histogram.html

https://altair-viz.github.io/gallery/select_detail.html

Hope this helps!

If you are good with switching to Altair. Here are a few articles you might leverage:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.