How to create a grouped bar chart?

Hi there,

I am trying to create a grouped bar chart using Streamlit, but without any success so far. All I got was a stacked bar chart.

Pandas DataFrame:

Job Stat Revenue Total Income
INV 100.00 150.00
WRK 200.00 250.00
CMP 300.00 350.00
JRB 400.00 450.00

The idea is to have the columns “Revenue” and “Total Income” grouped side by side, showing their respective amounts, like in the picture below:

I have tried Altair, also without any success. Is there any trick to achieve it?

Hi @julio,

Maybe something like the approach below, but with altair per this ref: https://stackoverflow.com/questions/43797379/how-to-create-a-grouped-bar-chart-in-altair. I haven’t used altair before, so did a quick seaborn example.

import seaborn as sns
import pandas as pd

revenue_df = df.drop('Total Income', axis=1)\
               .rename(columns={'Revenue': 'Value'})\
               .merge(pd.DataFrame(
                   {'Category': list(pd.np.repeat('Revenue', len(df)))}),
                   left_index=True,
                   right_index=True)

total_income_df = df.drop('Revenue', axis=1)\
                    .rename(columns={'Total Income': 'Value'})\
                    .merge(pd.DataFrame(
                        {'Category': list(pd.np.repeat('Total Income', len(df)))}),
                        left_index=True,
                        right_index=True)

df_revised = pd.concat([revenue_df, total_income_df])

# Display original df and grouped bar chart
df
sns.barplot(x="Job Stat", y="Value", hue="Category", data=df_revised)
st.pyplot()

1 Like