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

Show sub sample number in grouped box plots

The box plot is offering a lot of information. However, I could not find the option to display the same size in the parameters. So I made a small code snippet which appends this information as annotation. Hopefully this will help out anyone who has similar needs.

for i in df0[groupfilter].unique():
        name = str(i)
            "yshift": 20,
            "text": "sample size",
fig = {"data": traces, "layout": go.Layout(
title={'text': "text"}, xaxis={"type": 'category'},
annotations = annotations)}

Somehow drawing the arrowhead slows down process time by a bit so I have set it to False. I have shifted the number slightly upwards. I felt putting the number at the highest point makes more sense, but depending on your preference you could put it left or right etc. Also in my case I had to plot multiple box plots so the trace is indexed which may not be necessary.

I have revised and improved the current script to allow number to be switched on/off and also follow the trace color.

To make consistent coloring first import the default plotly color set (you can switch to the colors of your choice) and then do:

from plotly.colors import DEFAULT_PLOTLY_COLORS
colorway = [x for x in DEFAULT_PLOTLY_COLORS]
for i in items.unique():
    "font" : dict(color= colorway[i]),
    "yshift": 20,
    "text": 'total sample number here",
    "clicktoshow" : "onoff"})
fig = {"data": traces, "layout": go.Layout(
        annotations = annotations, colorway = colorway)}

The final result should looks something like this: