I have over 40 or so unique items in a dataframe column (e.g. 'a','b','c'......). I want to create a colour dictionary without having to type something like the following. So that each unique item has a unique colour - How can i create a dictionary like below which would enable me to give a unique colour to each item.
Any help would be greatly appreciated.
color_discrete_map = {'a':'rgb(42,9,4)',
'b':'rgb(111,203,209)',
'c':'rgb(55,165,172),',
'd':'rgb(29,127,136)',
'e':'rgb(2,84,92)',
'f':'rgb(4,37,42)',
#....
}
You can use seaborn.color_palette:
import seaborn as sns
sns.color_palette("husl", 10) # 10 colors

The returned object contains tuples with the RGB values:
>>> list(sns.color_palette("husl", 10))
[(0.9677975592919913, 0.44127456009157356, 0.5358103155058701),
(0.8616090647292522, 0.536495730113334, 0.19548899031476086),
(0.6804189127793346, 0.6151497514677574, 0.19405452111445337),
(0.46810256823426105, 0.6699492535792404, 0.1928958739904499),
(0.20125317221201128, 0.6907920815379025, 0.47966761189275336),
(0.21044753832183283, 0.6773105080456748, 0.6433941168468681),
(0.2197995660828324, 0.6625157876850336, 0.7732093159317209),
(0.433280341176423, 0.6065273407962815, 0.9585467098271748),
(0.8004936186423958, 0.47703363533737203, 0.9579547196007522),
(0.962272393509669, 0.3976451968965351, 0.8008274363432775)]
NB. 40 colors is a lot for the human eye to be able to discriminate all, but have a look at the options of the method, there are may possibilities
other examples.
"Set2":

"Paired":

Finally, an example of how to construct a dictionary:
dict(zip(list('ABCDEFG'), [tuple(int(c*255) for c in cs) for cs in sns.color_palette("husl", 7)]))
output:
{'A': (246, 112, 136),
'B': (197, 147, 49),
'C': (130, 168, 49),
'D': (52, 175, 137),
'E': (55, 170, 186),
'F': (128, 150, 244),
'G': (244, 93, 235)}
Simple enough, just grab a list of colours then join along the axis.
e.g
df_colors = pd.read_csv('https://raw.githubusercontent.com/codebrainz/color-names/master/output/colors.csv',header=None)
df_colors.columns = ['color_name', 'color_description', 'hex_value', 'r', 'g','b']
print(df_colors)
color_name color_description hex_value r g b
0 air_force_blue_raf Air Force Blue (Raf) #5d8aa8 93 138 168
1 air_force_blue_usaf Air Force Blue (Usaf) #00308f 0 48 143
2 air_superiority_blue Air Superiority Blue #72a0c1 114 160 193
3 alabama_crimson Alabama Crimson #a32638 163 38 56
4 alice_blue Alice Blue #f0f8ff 240 248 255
assuming your dataframe has unique values then you can just join along the indices.
print(df)
name
0 a
1 b
2 c
3 d
df1 = df.join(df_colors[['r','g','b']].astype(str).agg(','.join,axis=1).to_frame('rgb'),how='left')
print(df1)
name rgb
0 a 93,138,168
1 b 0,48,143
2 c 114,160,193
3 d 163,38,56
if you specifically want a dictionary, you can zip then after creating two iterables.
n = df['name'].unique()
c = df_colors[['r','g','b']].astype(str).agg(','.join,axis=1).tolist()
dict(zip(n,c))
{'a': '93,138,168', 'b': '0,48,143', 'c': '114,160,193', 'd': '163,38,56'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With