Black Lives Matter. Please consider donating to Black Girls Code today.

Grouped Box Plot Width

I’m having trouble setting the width of my box plots when there are more than a handful of box plots on one graphing area. The boxes get very thin and become unreadable but with large gaps in between them.

I’m looping through some data and creating a different trace for each one:

for x, hour in enumerate(hour_names):
            all_plots.append( go.Box(
                y = hourly_dict[hour].values,
                name = str(hour).zfill(2)+':00',
                boxmean = showmean,
                ))

And end up with something like:


That is totally unreadable (you can’t even tell its a box plot). So it would be good to widen the boxes.

I can set the width property using width = 0.8 but then the grouping no longer happens and I get this:

I’ve also played around with boxgap and boxgroupgap in the layout of the plot but it doesn’t fix the problem.
Is there anyway to still group the box plots and also set the width so they’re readable?

Hi @dig, why do you have to create a trace per box? If you create a trace per group as in the code below, the default settings looks quite good even with 24 x 2 boxes

import plotly.graph_objects as go
import numpy as np
N = 2000
n = 24
y = np.random.random((2, N))
fig = go.Figure()
fig.add_box(x=np.random.randint(0, n, N), y=y[0])
fig.add_box(x=np.random.randint(0, n, N), y=y[0])
fig.update_layout(boxmode='group')
fig.show()

Good question - it was a while ago I wrote the code for it. I think the data I was pulling in came in a format that made looping through easier. I’ll reformat it and give this a go. Thanks!

Thats worked a treat - thanks!