pythonplotlyplotly-express

Plotly express issue: box plosts shifted on the x axes when facetting


I am experiencing an unexpected behavior with plotly express when trying to plot a number of groups as facet box plot

import pandas as pd
import numpy as np

# Generate random data
np.random.seed(0)  # For reproducibility
group = ['A', 'B', 'C', 'D']
parameters = ['param1', 'param2', 'param3']
repetitions = 5  # Number of repetitions for each parameter on the same string

data = {
   'group': np.tile(np.repeat(strings, repetitions), len(parameters)),
   'parameter': np.repeat(parameters, len(strings) * repetitions),
   'value': np.random.rand(len(strings) * len(parameters) * repetitions) * 100  # Random values between 0 and 100
}
df_ = pd.DataFrame(data)

fig = px.box(df_, x = 'group', y= 'value', facet_row='parameter')
fig.show()

I would like the box plots to be all aligned on the x axes, I get that they have to be offset if I have multiple groups on the same axes but in this case makes no sense to me. How can I avoid this?

result


Solution

  • The box mode seems to be misaligned because the group is the default. Changing this to overlay will create them in the same position.

    fig = px.box(df_, x = 'group', y= 'value', facet_row='parameter', boxmode='overlay')
    

    enter image description here