Black Lives Matter. Please consider donating to Black Girls Code today.

Set Plotly legends' colors manually

Is there any solution to set individual legends’ colour manually ?
I have a chart as attached and I need to manually set the legend colours to be matched to another charts with same legends.
This is generated by using:

trace=[]
for colname, col in statusCountSorted[1:-1].iteritems():

trace.append(go.Bar(x=statusCountSorted.index, y=statusCountSorted[colname][0:-1],opacity=0.6,name=colname))
layout = go.Layout(
autosize=False,
width=800,
height=400,
barmode=‘stack’,
title = 'Top ‘+str(topCommonCount)+’ common issues from ‘+horizonStartDate+’ to ’ + endDate,
margin=go.Margin(
l=50,
r=50,
b=150,
t=50,
pad=0
))

Something like:

trace=[]
for colname, col in statusCountSorted[1:-1].iteritems():

  trace.append(go.Bar(
    x=statusCountSorted.index, 
    y=statusCountSorted[colname][0:-1],
    opacity=0.6,
    name='<span style="{}">{}</span>'.format(color, colname)
  ))

should do the trick.

Hi @etienne, thanks for getting back to me.
I tried your recommendation by defining color as follow:

color = [’#4682B4’, ‘#32CD32’, ‘#FF8000’,’#FF0000’]

trace=[]
for colname, col in statusCountSorted[1:-1].iteritems():
trace.append(go.Bar(x=statusCountSorted.index, y=statusCountSorted[colname][0:-1],opacity=0.6,
name=’{}’.format(color, colname)))

but it had no impact on the colors and they stayed same.
What do you think is missing ?

Also the Trace looks like this when I forced only one color for legend to test how it works:

Hi @etienne can I please have your comment on this

@p2ya - It looks like your passing an an array for colors. In

it was suggested to use a single color string. That is,

<span style="blue">Column name</span>

not

<span style="[blue]">Column name</span>

@chriddyp I made changes and I get the trace as:

The colors are not changed yet.

@p2ya Here are the support plans for Python: https://support.plot.ly/libraries/python

1 Like

Hi Jack, thanks for your response, so does it mean that I can not get help through community on this ?

If anyone interested this is now resolved by:
1- setting up a function for colour selection
2- setting the colour through:
marker=dict(
color=colorSelector(colname),
)
and adding it in the go.Bar()

function sample:
def colorSelector( colname ):
if colname == “New Issue”:
return '#FF0000
elif colname == “Closed Remotely”:
return ‘#4682B4