🚀 Gen 5 of the leading AI app deployment platform launches October 6. Click for the livestream.

Subplot not sharing same grouping

Hey all, I hope this is in the right place. I’m having an issue with some subplots I’m trying to create, whereby I’m not getting the right grouping that I want. It could be that I’ve been coding too long today and my brain is now fried, and maybe it’s something wrong with how I’ve structured my dataframe.

Here’s my figure. It’s basically what I want but notice how my department colours are not the same across all the plots, which results in duplicated entries in the legend. See below for my dataframe and figure code. I’ve been wrestling with this for a while and can’t seem to nail it down. Is my grouping incorrect? How can I get the departments to have the same colours for all 3 subplots?

df

# Subplot for position level by department

# Subset data for each position level
ic = df_poslevel_department[df_poslevel_department['PositionLevel']=='Individual Contributor']
mm = df_poslevel_department[df_poslevel_department['PositionLevel']=='Middle Management']
sm = df_poslevel_department[df_poslevel_department['PositionLevel']=='Senior Management']

# Put departments in list
department_list = df_poslevel_department['Department'].unique().tolist()

poslevel_by_department = make_subplots(rows=1, cols=3)

# Individual contributors
for d in department_list:
    poslevel_by_department.add_trace(
        go.Bar(x=df_poslevel_department['PositionLevel'][df_poslevel_department['PositionLevel']=='Individual Contributor'], 
               y=df_poslevel_department['Headcount'][(df_poslevel_department['PositionLevel']=='Individual Contributor') & (df_poslevel_department['Department']==d)], 
               name=d, 
               text=df_poslevel_department['Headcount'][(df_poslevel_department['PositionLevel']=='Individual Contributor') & (df_poslevel_department['Department']==d)]), 
        row=1, col=1
)

# Middle management
for d in department_list:
    poslevel_by_department.add_trace(
        go.Bar(x=df_poslevel_department['PositionLevel'][df_poslevel_department['PositionLevel']=='Middle Management'], 
               y=df_poslevel_department['Headcount'][(df_poslevel_department['PositionLevel']=='Middle Management') & (df_poslevel_department['Department']==d)], 
               name=d, 
               text=df_poslevel_department['Headcount'][(df_poslevel_department['PositionLevel']=='Middle Management') & (df_poslevel_department['Department']==d)]), 
        row=1, col=2
)


# Senior management
for d in department_list:
    poslevel_by_department.add_trace(
        go.Bar(x=sm['PositionLevel'], y=sm['Headcount'][sm['Department']==d], name=d, text=sm['Headcount'][sm['Department']==d]), row=1, col=3
)

poslevel_by_department.update_layout(title='Position Level by Department', title_x=0.45)