'Coloring points in a plotly box plot by column variable
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()
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
Solution 1:[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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DarkHark |


