Using two columns to define graph color

I have a dataframe where the columns are Sample, Size, Client, and Category.
I am trying to create a bar graph where:
x = Sample
y = Size
color = Client.

This is where I have the problem.
The column “Category” can be of two value: Normal or Outlier.
I want to use this column to override the color of the bar graphs.

As in, while I want the color of the bar to be separated by column Client, I want to color all the
Outlier bar with the same color.

Hi @wsher0901 ,

Welcome to the forum.

You can use combination color for Client and pattern_shape for Category.

Then, you can check different pattern shape on every trace.
Identify the value is Outlier or Normal for category column, then remove pattern and change the color.

For example below the Outlier will set color to ‘black’

import plotly.express as px 

df = ....

fig = px.bar(df, x="Sample", y="Size", color="Client",
             pattern_shape="Category",pattern_shape_sequence=["+", "x"])

# get to know first row in Category columns
# if first row is Outlier the shape will check use equal to  "+"
# if first row is Normal the shape will check use equal to "x"
# Outlier category will be colored 'black'
if df.at[0,"Category"] == "Outlier":
    fig.for_each_trace(lambda trace: trace.update(marker_color="black",marker_pattern_shape="") if trace.marker.pattern.shape == "+" else trace.update(marker_pattern_shape=""))

else:
    fig.for_each_trace(lambda trace: trace.update(marker_color="black",marker_pattern_shape="") if trace.marker.pattern.shape == "x" else trace.update(marker_pattern_shape=""))

Hope this help.