Change colors of box plot with custom percentile values

Hi All,

I’m trying to plot box plots with custom percentile values and below is the code:

import plotly.graph_objects as go

fig = go.Figure()

y = [[0.425, 0.425, 0.516, 0.525, 0.528, 0.528, 0.53, 0.533, 0.533, 0.534,
0.534, 0.535, 0.554, 0.568, 0.577, 0.585, 0.606, 0.606, 0.615, 0.626,
0.627, 0.627, 0.635, 0.653, 0.653, 0.669, 0.669, 0.675, 0.683, 0.691,
0.691, 0.693, 0.699, 0.699, 0.701, 0.711, 0.728, 0.733, 0.733, 0.81,
0.822, 0.822, 0.845, 0.845],
[0.526, 0.547, 0.582, 0.586, 0.640, 0.641, 0.653, 0.653, 0.654, 0.662,
0.672, 0.683, 0.685, 0.686, 0.686, 0.688, 0.690, 0.694, 0.696, 0.697,
0.697, 0.697, 0.700, 0.704, 0.707, 0.723, 0.741, 0.748, 0.751, 0.752,
0.766, 0.766, 0.768, 0.786, 0.787, 0.794, 0.798, 0.803, 0.806, 0.810,
0.834, 0.858, 0.889, 0.897],
[0.529, 0.533, 0.536, 0.540, 0.546, 0.547, 0.563, 0.563, 0.564, 0.564,
0.582, 0.592, 0.593, 0.613, 0.640, 0.641, 0.643, 0.646, 0.648, 0.651,
0.655, 0.667, 0.669, 0.669, 0.671, 0.678, 0.683, 0.699, 0.704, 0.708,
0.719, 0.726, 0.734, 0.748, 0.758, 0.765, 0.768, 0.771, 0.772, 0.772,
0.788, 0.805, 0.812, 0.823],
[0.657, 0.664, 0.665, 0.690, 0.705, 0.713, 0.737, 0.742, 0.744, 0.745,
0.746, 0.749, 0.758, 0.764, 0.765, 0.765, 0.766, 0.771, 0.771, 0.777,
0.785, 0.785, 0.789, 0.796, 0.800, 0.800, 0.806, 0.809, 0.809, 0.810,
0.813, 0.816, 0.816, 0.820, 0.821, 0.835, 0.849, 0.858, 0.858, 0.858,
0.880, 0.886, 0.896, 0.897]]

x = ['A', 'B', 'C', 'D']

fig.add_trace(go.Box(y=y, x=x))

fig.update_traces(q1=[0.535, 0.680, 0.590, 0.748], median=[0.631, 0.699, 0.668, 0.787],
                  q3=[0.699, 0.773, 0.738, 0.817], lowerfence=[0.425, 0.526, 0.529, 0.657],
                  upperfence=[0.845, 0.897, 0.823, 0.897])

fig.update_xaxes(
    mirror=True,
    ticks="outside",
    showline=True,
    linecolor="black",
    gridcolor="grey",
    showgrid=False,
)

fig.update_yaxes(
    mirror=True,
    ticks="outside",
    showline=True,
    linecolor="black",
    gridcolor="rgb(220,220,220)",
    showgrid=True
)

fig.update_layout(
    plot_bgcolor="white",
    showlegend=False,
    boxgap=0.2,
    boxmode="overlay",
    font=dict(family="serif", size=28, color="black"),
    yaxis=dict(tickfont=dict(family="serif", size=28, color="black")),
    xaxis=dict(tickfont=dict(family="serif", size=28, color="black")),
    yaxis_range=[0,1]
)

fig.show()

The graph looks like below:

Is there a way to change each box color?

Thank you!

The doc suggests you need to use plotly.graph_objects.Box(). There’s an example ‘Grouped Box Plots’ showing how at:

@davidharris Thank you for the reply. I have checked this page and I don’t need the grouping function. Is there a way to change the color without grouping? Please let me know if I’m missing any point here.

Thank you!