Connect Sidebar Filter to other variables

Okay, so I’m new to streamlit and have not coded python in a while so this might be really obvious and I just don’t get it. Anyway, what I’m trying to do is build an app that displays a stress-strain curve. In the sidebar I want to be able to select and unselect specs and then have the graphs show up or not.

My data is saved in three variables. The first is a list that includes all the specs for which I want to filter. The second includes the strain data for all samples, and the third includes the stress data. Sample 1 is stored in each first column, sample 2 in each second and so on. Also, between the samples the lengths are not static, so sample 1 is longer than sample 2. Oh and at the moment the titles of the columns of stress and strain are only indices, I tried the command data.columns = [spec] but that didn’t really help me.

Steps to reproduce

Code snippet:

st.sidebar.header('Enter filter (click or type)')

spec = st.sidebar.multiselect(
    'Select spec:',
    options = df['Spec'].unique(),
    default = df['Spec'].unique()

df_selection = df.query(
    'Spec == @spec'

I have set up the sidebox, but I don’t really know how to connect the df_selection to my stress and strain curves. Also, can I use a for loop to generate the plot?

Hope that is somehow understandable. Thanks already for any input

It is not understandable to me. Your code seems to work with the data I made up.

Oh yeah, I know that the code works. I want to use the “Spec” as basis for a filter. And I don’t know how to connect the Spec to the data that I have in my two variables. And I was hoping for input in that regard.
Sorry if that was confusing.

That is what worries me too, I can’t tell from your description how the specs are related to the stress-strain data.

Does this help to make sense of it? Each column of the stress variable and the strain variable is equal to one line of the spec variable.
I could also combine stress and strain in one variable, if that is necessary.

So the df in your code has just one column? And how are the stress and strain data stored in the program?

Well, not quite. In my df I have the spec, then the stress and the strain. Then I separate the values into unique variables. Through the .unique filtering, I get the specs in the list and through finding the start indices for the others, I have a DataFrame for stress and strain individually. As the variables are not of the same length, I thought this was the best way, cause later on I want to plot strain (xaxis) against stress (yaxis).

So I have the Mastervariable that includes the specs, stress and strain and then I separate them into the list spec, and the Dataframes Stress and Strain that each have a column for each spec.

Then df_selection should already have the data you need, doesn’t it? I don’t understand what the issue is.

Yes, df_selection does have all the data I need, in theory. But the structure of the data is not correct I think. If I plot the data in df_selection as it is now, I don’t get individual graphs for each spec but one giant mess. So I thought it was more efficient to separate the data into individual variables and then plot them. But as I said, I’m new to this, so this might not be the most efficient way to work. If you have a different suggestion, I’m happy for any input!

So I want to connect the first columns of the stress and strain data to the first line of the specs data. Then the second line to the second columns and so on. And I want to be able to filter for the specs and turn them off and on in the plot of stress and strain

This is how you can do it using matplotlib:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot()
df_selection.groupby("Spec").plot(x="Strain", y="Stress", legend=True, ax=ax)
1 Like

Wow, that’s it!!! Thanks so much!!! Sorry about the confusion. I didn’t expect this so be so few lines of code! Fantastic, thanks

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