# Conditionally apply different colors to each trace in a box plot figure in plotly dash express

**URL:** <https://community.plotly.com/t/conditionally-apply-different-colors-to-each-trace-in-a-box-plot-figure-in-plotly-dash-express/48793>\
**Category:** Dash Python\
**Created:** [January 8, 2021, 12:04am UTC](https://community.plotly.com/t/conditionally-apply-different-colors-to-each-trace-in-a-box-plot-figure-in-plotly-dash-express/48793 "2021-01-08T00:04:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rotortorot](https://avatars.discourse-cdn.com/v4/letter/r/96bed5/32.png) [@rotortorot](https://community.plotly.com/u/rotortorot)\
**Post date:** [January 8, 2021, 12:04am UTC](https://community.plotly.com/t/conditionally-apply-different-colors-to-each-trace-in-a-box-plot-figure-in-plotly-dash-express/48793/1 "2021-01-08T00:04:34Z")

</div>

Hi,

I have a Pandas data frame (variable name is data) as follows with dynamic number of rows and columns:

```auto
[244 rows x 8 columns]
     2021/1/7 22:30:21 2021/1/7 22:30:32
0 -0.128906 -0.125000
1 -0.125000 -0.128906
2 -0.125000 -0.128906
3 -0.121094 -0.128906
4 -0.125000 -0.125000
.. ... ...
595 -0.125000 -0.125000
596 -0.125000 -0.125000
597 -0.125000 -0.125000
598 -0.128906 -0.125000
599 -0.128906 -0.128906

```

I was able to plot this data as a box plot in **plotly dash app** using the following code:

```auto
df = pd.DataFrame.from_dict(data, orient='index')
df = df.transpose()
figure = px.box(
    df
)

```

 ![Capture](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/9/917eaccfc93839249d73a3edfab56cfc85922ad0.png)

I’m trying to change the box plot where the color of each trace depends on the column name.  
**Example:** The trace color should be red if the column name ends in a ‘1’ or green if it ends in a ‘2’.

I’ve had success in updating the color using **figure.for\_each\_trace()** as follows:

```auto
figure.for_each_trace(
    lambda trace: trace.update(fillcolor='red')
)

```

 ![Capture1](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/1/1a7e5d7216bf8313d7f27540b60ff8f73fa62afa.png)

I thought of adding a named row at the end of the dataframe and appending the color name manually while creating the dataframe, to use it as the trace color.

```auto
df.loc['colors']=['red', 'yellow']

New dataframe with this added row is shown below:
[600 rows x 2 columns]
       2021/1/7 22:30:21 2021/1/7 22:30:32
0 -0.128906 -0.125
1 -0.125 -0.128906
2 -0.125 -0.128906
3 -0.121094 -0.128906
4 -0.125 -0.125
... ... ...
596 -0.125 -0.125
597 -0.125 -0.125
598 -0.128906 -0.125
599 -0.128906 -0.128906
colors red yellow

```

I’ve been trying to access the colors appended manually from within the for\_each\_trace method but been wildly unsuccessful. See my horrible attempt at trying this shown below:

```auto
figure.for_each_trace(
    lambda trace: trace.update(fillcolor=df.loc['colors'][trace.uid])
)

```

Am I going in the right path? Any help is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![rotortorot](https://avatars.discourse-cdn.com/v4/letter/r/96bed5/32.png) [@rotortorot](https://community.plotly.com/u/rotortorot)\
**Post date:** [January 11, 2021, 7:27pm UTC](https://community.plotly.com/t/conditionally-apply-different-colors-to-each-trace-in-a-box-plot-figure-in-plotly-dash-express/48793/2 "2021-01-11T19:27:01Z")

</div>

Hi community!

Any help is greatly appreciated. I’m still stuck with this issue!
