Search code examples
pythonplotlyplotly-python

Plotly box plot: remove transparency


Boxplots in plotly by default are semi-transparent, how can I turn this off please. This has been asked on plotly forums before, but I'm not quite sure how to implement the suggestion. Below is a simple box plot from plotly doc, if someone can please help me remove any transparency from this.

import plotly.graph_objects as go
import numpy as np
np.random.seed(1)

y0 = np.random.randn(50) - 1
y1 = np.random.randn(50) + 1

fig = go.Figure()
fig.add_trace(go.Box(y=y0))
fig.add_trace(go.Box(y=y1))

fig.show()

Solution

  • The easiest way is to assign the fillcolor manually for each trace, which can be done by loading a color palette from plotly or plotly.express (see Color Sequences in Plotly), for example using the D3 palette :

    np.random.seed(1)
    
    y0 = np.random.randn(50) - 1
    y1 = np.random.randn(50) + 1
    
    colors = plotly.colors.qualitative.D3
    
    fig = go.Figure(data=[
        go.Box(y=y0, fillcolor=colors[0]),
        go.Box(y=y1, fillcolor=colors[1])
    ])
    
    fig.show()