(Python 3.11.3) Filtering two datasets and plotting using same multiselect filters but just first plot being shown

Hi,

I am new to streamlit and python, so sorry if this is a silly mistake. I am trying to produce a dashboard with common filters for different plots. This is the code I have:

# Getting locations
locations = sorted(gva_series.ltla22nm.unique())
years = sorted(gva_series.year.unique())

# st.sidebar.markdown("### GDHI")
select_dist = st.sidebar.multiselect('District', locations,key='2')
select_year = st.sidebar.multiselect('Years', years,key='3')

# Graphs
gva_series_filt = gva_series[gva_series['ltla22nm'].isin(select_dist) &
                             gva_series['year'].isin(select_year)]

fig = px.line(gva_series_filt, x="year", y="GVA", color="ltla22nm",
               title='GVA per hour')
st.plotly_chart(fig)

pay_series_filt = pay_series[pay_series['ltla22nm'].isin(select_dist) & pay_series['year'].isin(select_year)]
fig = px.line(pay_series_filt, x="year", y="GDHI", color="ltla22nm")
st.plotly_chart(fig)

The problem I have is that only the first graph is being affected by the filter, it doesnโ€™t matter which one is first.

Hey @Eliza, welcome to our forum :balloon: and congrats for getting your first app out there already.

My intuition here: it seems like both charts actually work. But the one that uses pay_series_filt dataframe doesnโ€™t show any line because the data might be empty after you do the filtering.

Do you mind checking what is present in both dataframes, i.e. by adding those calls?

st.write("pay_series_filt")
st.dataframe(pay_series_filt)

st.write("gva_series_filt")
st.dataframe(gva_series_filt)

Thank you so much! The issue was that the datasets had the year column of different types (numeric and str). After correcting that, it now works!

1 Like

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