Plotly: How to handle uneven gaps between categories in a box plot?

I couldn’t quite figure out why your go.Figure turns out the way it does. But if you reshape your data from wide to long and unleash you’ll get a shorter, cleaner code and arguably a much better visual result. We can talk more details later, but you’ll find a complete snippet right after this plot:

enter image description here

Complete code:

import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly
import as px

colors = {'A':plotly.colors.qualitative.Plotly[0], 

models = ['modelA', 'modelA', 'modelA', 'modelA', 'modelA', 'modelB', 'modelB', 'modelC', 'modelC', 'modelB', ]
samples = ['A', 'B', 'C', 'D', 'E', 'A', 'B', 'B', 'D', 'C']
score_cols = ['score_{}'.format(x) for x in range(10)]
scores = [(np.random.normal(mu, sd, 10).tolist()) for mu, sd in zip((np.random.normal(.90, .06, 10)), [.06]*10)]
data = dict(zip(score_cols, scores))
data['model'] = models
data['sample'] = samples

df = pd.DataFrame(data)

df_long = pd.wide_to_long(df, stubnames="score",
                          i=['model', 'sample'], j='type',
                          sep='_', suffix='\w+').reset_index()

fig =, x='model', y="score", color="sample")

Leave a Comment