Grouped Box Plots with more than 2 categories

Tried to use this as reference for creating a 7-category box plot (each category is a weekday).

However, I’m getting as output a time-series line plot for some reason, and can’t understand why.

Here’s the dataframe structure:

index    date               A                    B               weekday_w              weekday
0         2016-01-02        3.22                6.56             Monday                 0
1         2016-01-03        1.74                7.10             Tuesday                1
2         2016-01-04        0.53                5.67             Wednesday              2
3         2016-01-05        2.67                7.50             Thursday               3

Here’s my code:

df= df.sort_values(by='weekday')
x = df['weekday_w'].tolist() #returns list of strings ['Sunday','Sunday','Monday'....]
    y1 = df['A'].tolist() # returns list of numericals
    y2 = df['B'].tolist() # same

trace1= go.Box(
    y=y1,
    x=x,
    name='y1',
    marker=dict(
        color='#3D9970'
    )
)
    
trace2 = go.Box(
    y=y2,
    x=x,
    name='y2',
    marker=dict(
        color='#FF4136'
    )
)
    
layout = go.Layout(
    yaxis=dict(
        title='Time Spent',
        zeroline=False
    ),
    boxmode='group'
)
    
fig2 = go.Figure(data=data, layout=layout)
py.iplot(fig2)

Suggestion how to get the desired plot (2 bars for each of seven days)?

Hi @guy,

This example isn’t reproducible so I can’t see what you’re seeing, but it looks like data isn’t defined in this code snippet. Maybe you’re ending up with traces from a different example?

-Jon

Correct @jmmease, it was a lack-of-attention mistake.
Thanks for pointing it out.

1 Like