# Customize bar colour depends on condition

**URL:** <https://community.plotly.com/t/customize-bar-colour-depends-on-condition/31577>\
**Category:** Dash Python\
**Created:** [November 19, 2019, 8:35pm UTC](https://community.plotly.com/t/customize-bar-colour-depends-on-condition/31577 "2019-11-19T20:35:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felton](https://avatars.discourse-cdn.com/v4/letter/f/13edae/32.png) [@Felton](https://community.plotly.com/u/Felton)\
**Post date:** [November 19, 2019, 8:35pm UTC](https://community.plotly.com/t/customize-bar-colour-depends-on-condition/31577/1 "2019-11-19T20:35:05Z")

</div>

Hi,

I’m trying to add a dataframe that is in the below format as a barchart.

```
ticker	value	bar_colour

```

0 NZDCAD -4.25 Green  
1 AUDCAD -3.96 Red  
2 EURCAD -2.86 Green  
3 GBPCHF -2.49 Black  
etc

I’m trying to update the figure via a callback. and the code I currently have is. Essentially I’m trying to loop through each data point and create its own barchart, then combining them. It looks quite inelegant but it is the solution proposed by this thread “[Different colors for bars in barchart by their value](https://community.plotly.com/t/different-colors-for-bars-in-barchart-by-their-value/6527/4)”

```
fig = make_subplots(rows=1, cols=1)

for index, label_df in df.iterrows():
    trace = go.Bar(x=[label_df['ticker']],
                       y=[label_df['value']],
                       marker={'color': label_df['bar_colour']})
    fig.add_trace(trace, row=1, col=1,secondary_y=False)

return fig

```

The chart however is empty, with the y axis looks very funny and I think is somehow is the problem.

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/7/7b66649aab3cb3145a6827c5568ec473a25b0001.png)

So my question is two part

1. Is there an easier way to achieve my goal?
2. Why isn’t my code working?

Thanks very much

Felton

---

<div class="post-metadata">

**Author:** ![Emmanuelle](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emmanuelle/32/7853_2.png) [@Emmanuelle](https://community.plotly.com/u/Emmanuelle)\
**Post date:** [November 19, 2019, 10:12pm UTC](https://community.plotly.com/t/customize-bar-colour-depends-on-condition/31577/2 "2019-11-19T22:12:19Z")

</div>

Your code is not working because you need to remove the external `[]` in `x` and `y`.

For a simpler way, you can just set `marker_color` to a column as follows (in this case you have a single trace):

```auto
import plotly.express as px
df = px.data.gapminder().query("year == 2007").query("continent=='Europe'")
df['color'] = 'red'
df['color'][5:10] = 'black'

import plotly.graph_objects as go
fig = go.Figure(
    go.Bar(x=df['country'],
                y=df['gdpPercap'],
                marker={'color': df['color']})
)
fig.show()

```

 ![image](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/9/9927b26d56ba9ff03ee08dfb3d64538b145667d4.png)

You can also use plotly.express to perform a groupby by color, and create a trace per color. This will order the bars by color

```auto
import plotly.express as px
df = px.data.gapminder().query("year == 2007").query("continent=='Europe'")
df['color'] = 'red'
df['color'][5:10] = 'black'

fig = px.bar(df, x='country', y='gdpPercap', color='color', color_discrete_sequence=df.color.unique())
fig.show()

```

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

---

<div class="post-metadata">

**Author:** ![Felton](https://avatars.discourse-cdn.com/v4/letter/f/13edae/32.png) [@Felton](https://community.plotly.com/u/Felton)\
**Post date:** [November 20, 2019, 10:33am UTC](https://community.plotly.com/t/customize-bar-colour-depends-on-condition/31577/3 "2019-11-20T10:33:41Z")

</div>

Thanks very much. I’ve managed to make it work. You are right, I didn’t need the for loop in my case.

---

<div class="post-metadata">

**Author:** ![Naian](https://avatars.discourse-cdn.com/v4/letter/n/a9a28c/32.png) [@Naian](https://community.plotly.com/u/Naian)\
**Post date:** [June 6, 2022, 7:22am UTC](https://community.plotly.com/t/customize-bar-colour-depends-on-condition/31577/4 "2022-06-06T07:22:50Z")

</div>

Thanks @Emmanuelle .
