# Add custom color to marker color for each year in a px.bar graph

**URL:** <https://community.plotly.com/t/add-custom-color-to-marker-color-for-each-year-in-a-px-bar-graph/60162>\
**Category:** 📊 Plotly Python\
**Created:** [January 17, 2022, 8:54pm UTC](https://community.plotly.com/t/add-custom-color-to-marker-color-for-each-year-in-a-px-bar-graph/60162 "2022-01-17T20:54:20Z")\
**Posts on this page:** 2\
**Page:** 1

<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, 8:54pm UTC](https://community.plotly.com/t/add-custom-color-to-marker-color-for-each-year-in-a-px-bar-graph/60162/1 "2022-01-17T20:54:21Z")

</div>

I am trying to assign a custom colour to my Plotly Express Bar graph  
I create the plot fine and it displays the data as I would like it. It just won’t allow me to specify the colours for the bars.

 ![correct-plot-wrong-colors](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/b/8/b8985e29baddca19f52bd006e4312a907ffac93f.png)

If I try

```auto
fig.update_traces(marker_color=['#071633', '#0DEFFF'], showlegend=False)

```

I get the following plot which is the right colours just not on the correct bars

 ![wrong](https://us1.discourse-cdn.com/flex024/uploads/plot/original/3X/4/7/47623f4aa2176e7ff67a23cc02036dfe9597ab57.png)

Can someone help me with assigning the colour #071633 to 2019 and #0DEFFF to 2020

My complete code for the chart creating and data is as follows.

```auto

# Our imaginary Data to be used to create our Pandas Series
dict_2019 = {'Legal' : 70,
           'Police' : 90,
           'Government' : 0, 
           'Army' : 70,
           'Navy' : 58,
           'Air Force' : 25,
           'Coastguard' : 0,
           'Ambulance' : 6,
           'Counter-Terror' : 39,
           'Direct Action' : 12
          }

dict_2020 = {'Legal' : 95,
           'Police' : 50,
           'Government' : 24,
           'Army' : 95,
           'Navy' : 0,
           'Air Force' : 65,
           'Coastguard' : 0,
           'Ambulance' : 0,
           'Counter-Terror' : 49,
           'Direct Action' : 25
          }

# Create our Pandas Series
sf1 = pd.Series(dict_2019)
sf2 = pd.Series(dict_2020)

# Merge our Pandas Series
df_merge = pd.concat([sf1, sf2], axis = 1)

# Create our column names
df_merge.set_axis(column_names, axis = 'columns', inplace=True)

# Assign our new column names to the df_merge dataframe
df_merge.set_axis(column_names, axis = 'columns', inplace=True)

# Create our boolean mask
action_mask = (df_merge['2019'] > 0) & (df_merge['2020'] > 0)

# Draw our Plot
fig = px.bar(df_merge[action_mask])

# Change the bar mode
fig.update_layout(barmode='group',
                  title='<b>Number of actions by unit<b>',
                  xaxis_categoryorder = 'category ascending',
                  height = 800,
                  plot_bgcolor='rgba(0,0,0,0)',
                  showlegend=True
                 )

fig.update_traces(marker_color=['#071633', '#0DEFFF'], showlegend=True)

fig.update_xaxes(tickangle=45,
                 title_text='<b>Unit<b>',
                 showline=True,
                 linewidth=1,
                 linecolor='#071633'
                )

fig.update_yaxes(title_text='<b>Count<b>',
                 showline=True,
                 linewidth=0.25,
                 linecolor='#071633')

fig.show() 

```

---

<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, 10:06pm UTC](https://community.plotly.com/t/add-custom-color-to-marker-color-for-each-year-in-a-px-bar-graph/60162/2 "2022-01-17T22:06:03Z")

</div>

Typical that having postwed a question an answer appears in another post from months ago

> [@Plotly express bar colour change](https://community.plotly.com/t/plotly-express-bar-colour-change/36210/5):
>
> 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: 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' } )

I just needed to add a color\_discrete\_mapp to my px.bar and it allows me to assign colors to my columns.

Working graphing code is as follows

```Python
fig = px.bar(df_merge[action_mask],
            color_discrete_map={
                '2019' : '#071633',
                '2020' : '#0DEFFF'
            }
            )

# Change the bar mode
fig.update_layout(barmode='group',
                  title='<b>Number of actions by unit<b>',
                  xaxis_categoryorder = 'category ascending',
                  height = 800,
                  plot_bgcolor='rgba(0,0,0,0)',
                  showlegend=True
                 )

fig.update_xaxes(tickangle=45,
                 title_text='<b>Unit<b>',
                 showline=True,
                 linewidth=1,
                 linecolor='#071633'
                )

fig.update_yaxes(title_text='<b>Count<b>',
                 showline=True,
                 linewidth=0.25,
                 linecolor='#071633')

fig.show()
``
```
