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

How to group boxes this way in boxplots?

I’ve reviewed the boxplot documentation and the workaround for supplying precomputing statistics for boxplots ( Yet I still cannot achieve the following. Does anyone know how to achieve this grouping and coloring of boxes on a single plot?


1 Like

gets you somewhat close. But to color the box per-group (as opposed to per-trace), you’ll need to split your data into 6 traces as in

Thanks for the quick and helpful reply, Etienne! I was able to get closer to what I’m looking for, but ran into some troubles.

  1. How can I get labels under each box, as opposed to labeling each “group?”
  2. More importantly, how do I eliminate the huge spaces between groups? (See image. I’ve already reduced the boxgap and boxgroupgap to 0.1 each.) When I have 1 item per group, the bar width is excellent (it fills the whole space for that group. As soon as I add another item, all the boxes become really skinny with all this margin/padding.

Can you share a code snippet of what you tried so far to help us help you?

Here’s basically what I’ve done, since the grouping is more logical:

Using the grouping suggested by your codepen, I get better spacing but still way too much space between boxes:

As soon as I go up to 5 or 6 groups, I end up with super skinny boxplots that are hard to read. Here’s the best I’ve been able to come up with; notice that the bars are super skinny with too much space between them.



Hmm that’s confusing. It might be easier to set boxmode: 'overlay' and set the box position (using x0) one by one as in

Thanks for the suggestion. This looks like it requires prior knowledge of how many items there are in each group. Sometimes I might have 1, 2, 3, 4 or perhaps 0 items. Is there a way to do this that wouldn’t require manually offsetting positions?

1 Like

I had a similar problem. I was trying to subset the data and then plot each subset as a grouped boxplot.

The only solution I could get was to plot each set of data as a seperate plot. For some reason it leaves the space for all the diffrent groups on one plot. I had to create a new dataframe within each plot for the data specific to it.

I hope this helps

Hi we need to use pivot to make this kind of graph i had plotted this