1

I am looking to change the color of the points in the box plot based on a column value from the data frame (committed).

users = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']
cost = [87.25, 84.69, 82.32, 87.25, 84.59, 82.32, 89.45, 86.37, 83.83]
committed = ['f', 'f', 't', 't', 'f', 'f', 'f', 't', 'f']
df = pd.DataFrame({'user': users, 'cost': cost, 'committed': committed})

To get the below plot, I entered this:

fig = go.Figure(
    data=[go.Box(
        x=df['user'],
        y=df['cost'],
        boxpoints='all',
        jitter=.3,
        pointpos=-1.8,
        )]
)
fig.show()

example box plot

I have found this post for r and was hoping in the past 5 years, something has changed to make it possible: Coloring jitters in a plotly box plot by group variable

I have tried this, but it does not appear that the marker or marker_color accepts what I attempted and I am unsure how to set the values otherwise.

def set_color(point):
    if point == 't':
        return "red"
    elif point == 'f':
        return "blue"

fig = go.Figure(
    data=[go.Box(
        x=df['user'],
        y=df['cost'],
        boxpoints='all',
        jitter=.3,
        pointpos=-1.8,
        marker=dict(color=list(map(set_color, df['committed'])))
        )]
)
fig.show()

Error:

Invalid value of type 'builtins.list' received for the 'color' property of box.marker
DarkHark
  • 614
  • 1
  • 6
  • 20

1 Answers1

1

I ended up finding my answer here: Plotting data points over a box plot with specific colors & jitter in plotly

My solution:

import plotly.express as px
import plotly.graph_objects as go

fig = px.strip(
    df, x='user', y='cost', color='committed', stripmode="overlay"
)

for user in df['user'].unique():
    fig.add_trace(go.Box(y=df.query(f'user == "{user}"')['cost'], name=user, marker_color="aquamarine"))

fig.show()

enter image description here

DarkHark
  • 614
  • 1
  • 6
  • 20