# Px.bar color and legend

**URL:** <https://community.plotly.com/t/px-bar-color-and-legend/50749>\
**Category:** Dash Python\
**Created:** [March 7, 2021, 12:19pm UTC](https://community.plotly.com/t/px-bar-color-and-legend/50749 "2021-03-07T12:19:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHaag](https://avatars.discourse-cdn.com/v4/letter/i/7bcc69/32.png) [@iHaag](https://community.plotly.com/u/iHaag)\
**Post date:** [March 7, 2021, 12:19pm UTC](https://community.plotly.com/t/px-bar-color-and-legend/50749/1 "2021-03-07T12:19:25Z")

</div>

hi all, I have the following code, im wanting to make a legend like this from

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

def SetColor(y):  
if(y \<= 1):  
return “red”  
elif(y \<= 3):  
return “yellow”  
elif(y \<= 5):  
return “green”  
customscale=[[1, “rgb(255, 0, 0)”],  
[2, “rgb(255, 0, 0)”],  
[3, “rgb(0, 0, 255)”],  
[4, “rgb(0, 0, 255)”]]  
@app.callback(  
Output(component\_id=“chart”, component\_property=“figure”),  
Input(component\_id=“option”, component\_property=“value”))  
def on\_option(option):  
filtered\_df = df[(df[“Class1”]==option) | (df[“Class2”]==option) | (df[“Class3”]==option) | (df[“Class4”]==option) | (df[“Class5”]==option)]  
return px.bar(filtered\_df, x=filtered\_df[“ID”], y=filtered\_df[“score”]) #color=dict(list(map(SetColor, filtered\_df[‘score’])))) #color\_continuous\_scale=“Viridis”, range\_color=(0, 7))

cannot seem to work this out… can this be done with plotly express??

---

<div class="post-metadata">

**Author:** ![iHaag](https://avatars.discourse-cdn.com/v4/letter/i/7bcc69/32.png) [@iHaag](https://community.plotly.com/u/iHaag)\
**Post date:** [March 7, 2021, 4:33pm UTC](https://community.plotly.com/t/px-bar-color-and-legend/50749/2 "2021-03-07T16:33:33Z")

</div>

Okay, i got it woring with , ).update\_traces(marker = dict(color=list(map(SetColor, filtered\_df[‘Score’]))))

Now, the legend…

---

<div class="post-metadata">

**Author:** ![iHaag](https://avatars.discourse-cdn.com/v4/letter/i/7bcc69/32.png) [@iHaag](https://community.plotly.com/u/iHaag)\
**Post date:** [March 8, 2021, 1:51pm UTC](https://community.plotly.com/t/px-bar-color-and-legend/50749/3 "2021-03-08T13:51:33Z")

</div>

def SetColor(y):  
if(y \<= 1):  
return “red”  
elif(y \<= 2):  
return “orange”  
elif(y \<= 3):  
return “yellow”  
elif(y \<= 4):  
return “lightgreen”  
elif(y \<= 5 | 6):  
return “green”  
elif(y \<= 7):  
return “darkgreen”  
elif(y \<= 8):  
return “silver”  
elif(y \<= 9):  
return “gold”

labels = [‘Very low (1)’, ‘Low’, ‘Below average (3)’, ‘Average(4)’, ‘Adverage (5, 6)’, ‘Above average (7)’, ‘High (8)’, ‘Very High (9)’]  
colors = {‘Very low (1)’: ‘red’,  
‘Low (2)’: ‘orange’,  
‘Below average (3)’: ‘yellow’,  
‘Average (4)’: ‘lightgreen’,  
‘Average (5, 6)’: “green”,  
‘Above average (7)’: ‘darkgreen’,  
‘High (8)’: ‘silver’,  
‘Very High (9)’: ‘gold’}
