# Plotly express bar colour change

**URL:** <https://community.plotly.com/t/plotly-express-bar-colour-change/36210>\
**Category:** 📊 Plotly Python\
**Created:** [March 14, 2020, 10:15am UTC](https://community.plotly.com/t/plotly-express-bar-colour-change/36210 "2020-03-14T10:15:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kashi](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@kashi](https://community.plotly.com/u/kashi)\
**Post date:** [March 14, 2020, 10:15am UTC](https://community.plotly.com/t/plotly-express-bar-colour-change/36210/1 "2020-03-14T10:15:21Z")

</div>

How can I change the colour of the plotly express bar-chart to green in the following code?

```auto
import plotly.express as px
import pandas as pd

# prepare the dataframe
df = pd.DataFrame(dict(
        x=[1, 2, 3],
        y=[1, 3, 2]
    ))

# prepare the layout
title = "A Bar Chart from Plotly Express"

fig = px.bar(df, 
             x='x', y='y', # data from df columns
             color= pd.Series('green', index=range(len(df))), # does not work
             title=title,
             labels={'x': 'Some X', 'y':'Some Y'})
fig.show()

```

---

<div class="post-metadata">

**Author:** ![empet](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@empet](https://community.plotly.com/u/empet)\
**Post date:** [March 14, 2020, 11:13am UTC](https://community.plotly.com/t/plotly-express-bar-colour-change/36210/2 "2020-03-14T11:13:12Z")

</div>

Hi @kashi  
Welcome to Plotly forum!!!  
Before working with plotly express you should have some background in plotly.py.

A figure consists in data, and layout (while animations have also, frames). Data is a list of traces such as  
scatter, bar, heatmap, etc Each trace has a few attributes that can be updated after your fig was defined.

As long as you have no knowledge on plotly.py you are not able to update your figures according to your preferences.

In the case of your code above you should perform the following update:

```auto
fig.update_traces(marker_color='green')

```

To get info on Bar attributes just type:

```auto
help(go.Bar)

```

and similarly on any trace type.  
To update the figure appearance (i.e to improve its aesthetics) you can update its layout. Just try it for your example:

```auto
fig.update_layout(width=700, height=500, bargap=0.05)

```

---

<div class="post-metadata">

**Author:** ![kashi](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@kashi](https://community.plotly.com/u/kashi)\
**Post date:** [March 15, 2020, 3:59am UTC](https://community.plotly.com/t/plotly-express-bar-colour-change/36210/3 "2020-03-15T03:59:00Z")

</div>

Thanks @empet. I did go through the background documents.

So, do you mean to say that the only way I can change the color of a bar created by plotly express is after it gets instantiated? Is there no property within px to change it?

---

<div class="post-metadata">

**Author:** ![empet](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@empet](https://community.plotly.com/u/empet)\
**Post date:** [March 15, 2020, 7:35am UTC](https://community.plotly.com/t/plotly-express-bar-colour-change/36210/4 "2020-03-15T07:35:09Z")

</div>

@kashi

You can assign the same color to each bar setting `color_discrete_sequence =['green']*3`:

```auto
fig = px.bar(df, 
             x='x', y='y', 
             color_discrete_sequence =['green']*len(df),
             title=title,
             labels={'x': 'Some X', 'y':'Some Y'})

```

For more details on `px.bar` see [https://plot.ly/python-api-reference/generated/plotly.express.bar.html](https://plot.ly/python-api-reference/generated/plotly.express.bar.html).

---

<div class="post-metadata">

**Author:** ![brendanmorin](https://avatars.discourse-cdn.com/v4/letter/b/b3f665/32.png) [@brendanmorin](https://community.plotly.com/u/brendanmorin)\
**Post date:** [February 9, 2021, 12:09am UTC](https://community.plotly.com/t/plotly-express-bar-colour-change/36210/5 "2021-02-09T00:09:07Z")

</div>

Not sure when this functionality was added, for anyone coming here looking for solutions to grouped bar charts, what most people will want in this case is not `color_discrete_sequence` but instead `color_discrete_map`. For example:

```auto
fig = px.bar(
    df, 
    x='x', 
    y='y', 
    color='z', # if values in column z = 'some_group' and 'some_other_group'
    color_discrete_map={
        'some_group': 'red',
        'some_other_group': 'green'
    }
)

```

---

<div class="post-metadata">

**Author:** ![NeStack](https://avatars.discourse-cdn.com/v4/letter/n/e495f1/32.png) [@NeStack](https://community.plotly.com/u/NeStack)\
**Post date:** [December 13, 2021, 6:33pm UTC](https://community.plotly.com/t/plotly-express-bar-colour-change/36210/6 "2021-12-13T18:33:20Z")

</div>

Thank you so mucn \<3, exactly what I was looking for!

---

<div class="post-metadata">

**Author:** ![twelsh37](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/twelsh37/32/25892_2.png) [@twelsh37](https://community.plotly.com/u/twelsh37)\
**Post date:** [January 17, 2022, 9:59pm UTC](https://community.plotly.com/t/plotly-express-bar-colour-change/36210/7 "2022-01-17T21:59:38Z")

</div>

Dude!, you saved me. I’ll use this to answer the question I posed earlier

---

<div class="post-metadata">

**Author:** ![garyk630](https://avatars.discourse-cdn.com/v4/letter/g/c67d28/32.png) [@garyk630](https://community.plotly.com/u/garyk630)\
**Post date:** [March 3, 2022, 5:25pm UTC](https://community.plotly.com/t/plotly-express-bar-colour-change/36210/8 "2022-03-03T17:25:36Z")

</div>

Great answer.

---

<div class="post-metadata">

**Author:** ![Herodotus999](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/herodotus999/32/23946_2.png) [@Herodotus999](https://community.plotly.com/u/Herodotus999)\
**Post date:** [May 4, 2023, 8:03am UTC](https://community.plotly.com/t/plotly-express-bar-colour-change/36210/9 "2023-05-04T08:03:21Z")

</div>

```auto
fig.update_traces(marker_color='green')

```

How can I input RGB color or HEX color codes here?

---

<div class="post-metadata">

**Author:** ![empet](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@empet](https://community.plotly.com/u/empet)\
**Post date:** [May 4, 2023, 1:24pm UTC](https://community.plotly.com/t/plotly-express-bar-colour-change/36210/10 "2023-05-04T13:24:57Z")

</div>

Just replace the string `'green'` by a shade of green color code:

```auto
fig.update_traces(marker_color='#008000')

```

or

```auto
fig.update_traces(marker_color='rgb(0,128,0)')

```

---

<div class="post-metadata">

**Author:** ![Herodotus999](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/herodotus999/32/23946_2.png) [@Herodotus999](https://community.plotly.com/u/Herodotus999)\
**Post date:** [May 6, 2023, 5:31am UTC](https://community.plotly.com/t/plotly-express-bar-colour-change/36210/11 "2023-05-06T05:31:27Z")

</div>

Thank you very much 😃
