Color map to labels


#1

has anyone by any chance come up w a pattern for always adding the same color to a certain label in a pie/bar chart so that plotly doesnt automatically assign colors regardless of the order that they are in in the dataframe?

animals = ['Bear', 'Cat', 'Turtle', 'Dog']

The first color plotly chooses automatically would be orange (this probably isnt true btw) so it assigns it to ‘Bear’ btu what if i wanted ‘Bear’ to always be red even if animals looked like this instead:

animals = ['Dog', 'Turtle', 'Cat', 'Bear']

I’d like ‘Dog’ to always be blue, ‘Cat’ is always green, instead of having whatever is first in the list always be orange, for example

thank u


#2

You mentioned they are values in a dataframe… so assuming the column ‘animals’ exists you could try something like:
ani_type=np.unique(df[‘animals’].values).tolist()
animal_code={ani_type[i]: i for i in range(len(ani_type))}
color_vals=[animal_code[cl] for cl in df[‘animals’]]

p1_colorscale=[[0.0, ‘brown’],[0.2,‘brown’],[0.2,‘yellow’],[0.4,‘yellow’],[0.4,‘green’],[0.6,‘green’],[0.6,‘red’],[0.8,‘red’],[0.8,‘blue’][1,‘blue’]]

then your marker=dict(color=color_vals, colorscale=p1_colorscale, showscale=False)