My question is if there is a proper way to crosslink different line plots or even a folium map with each other. The possibilities i found until now (plotly_events and st.plotly_chart) do not seem to properly work with line charts.
Does anybody know of some approach that i overlooked or other possibilities to achieve this kind of interface? Maybe i have to create it outside of streamlit and then link to it?
You could also utilize combinations of on_select callbacks in st.plotly_chart to update the figures of other charts.
import streamlit as st
import numpy as np
import plotly.figure_factory as ff
# Add histogram data
x1 = np.random.randn(200) - 2
x2 = np.random.randn(200)
x3 = np.random.randn(200) + 2
# Group data together
hist_data = [x1, x2, x3]
group_labels = ['Group 1', 'Group 2', 'Group 3']
def on_select_callback_1():
# Access the selected data
chart1 = st.session_state.distplot1
selected_data = chart1.selection
# Do something with the selected data
# For example, update the second chart based on the first chart's selection
def on_select_callback_2():
# Access the selected data
chart2 = st.session_state.distplot2
selected_data = chart2.selection
# Do something with the selected data
# For example, update the first chart based on the second chart's selection
# Create distplot with custom bin_size
if 'fig1' not in st.session_state:
st.session_state.fig1 = ff.create_distplot(
hist_data, group_labels, bin_size=[.1, .25, .5])
if 'fig2' not in st.session_state:
st.session_state.fig2 = ff.create_distplot(
hist_data, group_labels, bin_size=[.2, .3, .6])
# Plot!
st.plotly_chart(st.session_state.fig1, on_select=on_select_callback_1, key="distplot1")
st.plotly_chart(st.session_state.fig2, on_select=on_select_callback_2, key="distplot2")
Thanks for the Feedback! This is a good approach but it doesn’t seem to work for line charts since the selection output stays empty after interaction with the linechart. Maybe i am missing something?
Example chart:
import streamlit as st
import plotly.express as px
import pandas as pd
# Create example dataset
data = {
'Year': ['2000', '2000', '2000', '2010', '2010', '2010', '2020', '2020', '2020'],
'Value': [100, 150, 200, 110, 160, 210, 120, 170, 220],
'pop': ['pop_1', 'pop_2', 'pop_3', 'pop_1', 'pop_2', 'pop_3', 'pop_1', 'pop_2', 'pop_3']
}
habitat_area = pd.DataFrame(data)
# Create line plot
fig = px.line(habitat_area, x='Year', y='Value', color='pop', title='Habitat Area')
# Display plot in Streamlit
event = st.plotly_chart(fig, key="habitat", on_select="rerun")
# Access the selected data
if event:
selected_data = event.selection
st.write("Selected data:", selected_data)
A new event is triggered after a selection is made on the chart. In case of a line chart, there is no option to make a selection directly on the chart itself. Additionally, making a selection in the legend does not initiate any events, even for other types of charts.
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.