Home > Enterprise >  plot a bar chart using groupby function and plotly and streamlit
plot a bar chart using groupby function and plotly and streamlit

Time:06-24

i am trying to plot a bar chart based on groupby function but once i try it crash and display the below error:

this error below appear when the user select 3 items from the multiselect widget.

ValueError: All arguments should have the same length. The length of argument color is 3, whereas the length of previously-processed arguments ['gender', 'count'] is 95

code:

some_columns_df = df.loc[:,['gender','country','city','hoby','company','status']]
some_collumns = some_columns_df.columns.tolist()

select_box_var= st.selectbox("Choose X Column",some_collumns)
multiselect_var= st.multiselect("Select Columns To GroupBy",some_collumns)  

test_g3 = df.groupby([select_box_var]   multiselect_var).size().reset_index(name='count')
fig = px.histogram(test_g3,x=select_box_var, y='count',color=multiselect_var ,barmode = 'group',text_auto = True)

            

I know the error is in the color parameter in the px.histogram

CodePudding user response:

The reason is color only accepts one category.

color=['column_a','column_b']

Would cause

ValueError: All arguments should have the same length. The length of argument color is 2, whereas the length of previously-processed arguments ['total_bill'] is 244

2 is the length of list ['column_a','column_b'], while 244 is the dataframe's rows.


According to the enter image description here



If ','.join didn't work, having issue,

categories = df[['sex','time']].agg(', '.join, axis=1)

then we try another way

categories = df['sex']   df['time']

Sup[1]

enter image description hereenter image description here

  • Related