how to plot data based on user selection using pandas ,plotly, python

Dataframe consist of 3 columns I want to plot the value_counts() of the selected column based on the user selection

Until now i am able to plot the the value_counts() of the hole selected column.

If the user select the column name from the first dropdown list and the second one is empty I need to plot all the values of the selected column.

If the user select the column name from the first dropdown list and select a value/s from the second one I need to plot the values that are selected in the second dropdown list.

example 1:

dp1 ==loc1
dp2== " "

expected result 1

Ploting the bar chart for all the values for the loc1

example 2:

dp1 ==loc1
dp2== ["cat1","cat3"]

expected result 2

Ploting the bar chart for all the values for the cat1,cat3 that are existing in loc1

code:

import pandas as pd
import streamlit as st 
import plotly.express as px
import plotly.graph_objs as go


df =pd.DataFrame({
                "source_number":[11199,11328,11287,32345,12342,1232,13456,123244,1235],
                "location":["loc1","loc2","loc3","loc1","loc1","loc2","loc3","loc2","loc1"],
                "category":["cat1","cat3","cat1","cat3","cat2","cat3","cat2","cat3","cat1"],
                })
interactive =  st.beta_container()    
col1,col2 = st.beta_columns(2)

type_of_analysis = ("Single Column","Multiple Columns")
vales_of_analysis_type= st.sidebar.radio("show Type of analysis",type_of_analysis)
st.sidebar.title("Select type of ploting")
box_chart = st.sidebar.checkbox("Bar Chart")
Pie_chart = st.sidebar.checkbox("Pie Chart")
timeSeries_chart = st.sidebar.checkbox("Time Series")


with interactive: 
    all_columns_names= df.columns.tolist()
    with col1:
        option1 = st.multiselect("select column to plot",all_columns_names)
              
    with col2:
        vals = set()

        if not isinstance(option1, list):
            option1 = [option1]

        for col in option1:
            vals.update(df[col].values.tolist())

        option2 = st.multiselect("select required value/s to plot", list(vals))
    
        if vales_of_analysis_type == "Single Column":
                if box_chart==True:
                    if st.button("generate plot"):
                        st.success("generating customizable  plot of bar chart for {}".format(option1))
                        if option2==" ": 
                            s = df[option1[0]].str.strip().value_counts()
                        else:
                            s=df[option2[0]].value_counts()
                        trace  = go.Bar(
                                x=s.index,
                                y=s.values,
                                showlegend = True
                                )

                        layout = go.Layout(
                            title = 'total of {}'.format(option1),
                        )
                        data = [trace]
                        fig = go.Figure(data=data,layout = layout)
                        st.plotly_chart(fig)


Read more here: https://stackoverflow.com/questions/66997737/how-to-plot-data-based-on-user-selection-using-pandas-plotly-python

Content Attribution

This content was originally published by user5980666 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: