Cufflinks also provides a wide set of tools for color managements; including color conversion across multiple spectrums and color table generation.

In [2]:
import cufflinks as cf

Colors can be represented as strings:

HEX "#db4052"
RGB "rgb(219, 64, 82)"
RGBA "rgba(219, 64, 82, 1.0)"

Color Conversions

In [ ]:
# The colors module includes a pre-defined set of commonly used colors
cf.colors.cnames
In [7]:
# HEX to RGB
cf.colors.hex_to_rgb('red')
Out[7]:
'rgb(219, 64, 82)'
In [8]:
# RGB to HEX
cf.colors.rgb_to_hex('rgb(219, 64, 82)')
Out[8]:
'#db4052'
In [10]:
# RGB or HEX to RGBA (transparency)
cf.colors.to_rgba('#3780bf',.5), cf.colors.to_rgba('rgb(219, 64, 82)',.4)
Out[10]:
('rgba(55, 128, 191, 0.5)', 'rgba(219, 64, 82, 0.4)')
In [11]:
# RGBA to RGB (flatten transparency)
# By default assumes that the transparency color is *white*, however this can be also passed as a parameter. 
cf.colors.rgba_to_rgb('rgba(219, 64, 82, 0.4)','white')
Out[11]:
'rgb(240, 178, 185)'

Normalization

In [12]:
# Cufflinks.colors.normalize will always return the an hex value for all types of colors
colors=['#f08','rgb(240, 178, 185)','rgba(219, 64, 82, 0.4)','green']
[cf.colors.normalize(c) for c in colors]
Out[12]:
['#ff0088', '#f0b2b9', '#f0b2b9', '#008000']

Color Ranges

A range of colors can be generated using a base color and varying the saturation.

In [13]:
# 10 different tones of pink
cf.colors.color_range('pink',10)
Out[13]:
['#000000',
 '#33001b',
 '#660036',
 '#990051',
 '#cc006c',
 '#ff0088',
 '#ff329f',
 '#ff65b7',
 '#ff99cf',
 '#ffcce7',
 '#ffffff']

Color Tables

This function is meant to be used in an iPython Notebook. It generates an HTML table to display either a defined list of colors or to automatically generate a range of colors.

In [14]:
# Displaying a table of defined colors (list)
colors=['#f08', 'rgb(240, 178, 185)', 'blue' , '#32ab60']
cf.colors.color_table(colors)
  • #FF0088
  • #F0B2B9
  • #3780BF
  • #32AB60
In [15]:
# Generating 15 shades of orange
cf.colors.color_table('orange',15)
  • #FFFFFF
  • #FFEEDD
  • #FFDDBB
  • #FFCC99
  • #FFBB77
  • #FFAA55
  • #FF9933
  • #FF9932
  • #FF8810
  • #EE8E2F
  • #CC7A28
  • #AA6521
  • #88511B
  • #663D14
  • #44280D
  • #221406
  • #000000

Color Generators

A color generator can be used to produce shades of colors in an iterative form. For example when plotting N timeseries so the color used are as distinctive as possible.

In [16]:
# Create a generator using 3 defined base colors
colors=['green','orange','blue']
gen=cf.colors.colorgen(colors)
outputColors=[gen.next() for _ in range(15)]
cf.colors.color_table(outputColors)
  • #008000
  • #FF9933
  • #3780BF
  • #4B8CC5
  • #198C19
  • #FFA347
  • #FFAD5B
  • #5F99CB
  • #339933
  • #4CA64C
  • #FFB770
  • #73A6D2
  • #87B2D8
  • #66B266
  • #FFC184
In [17]:
# Create a generator with default set of colors
gen=cf.colors.colorgen()
outputColors=[gen.next() for _ in range(15)]
cf.colors.color_table(outputColors)
  • #E24A33
  • #3E6FB0
  • #8476CA
  • #656565
  • #FBC15E
  • #8EBA42
  • #FFB5B8
  • #FFBCBF
  • #E45C47
  • #517DB7
  • #9083CF
  • #747474
  • #FBC76E
  • #99C054
  • #A4C767
In [ ]:
# We can see all available scales with
cf.get_scales()
In [19]:
# Other color scales can be also seen here
cf.colors.scales()
accent
blues
brbg
bugn
bupu
dark2
dflt
ggplot
gnbu
greens
greys
oranges
orrd
paired
pastel1
pastel2
piyg
prgn
pubu
pubugn
puor
purd
purples
rdbu
rdgy
rdpu
rdylbu
rdylgn
reds
set1
set2
set3
spectral
ylgn
ylgnbu
ylorbr
ylorrd
In [20]:
colorscale=cf.colors.get_scales('accent')
cf.colors.color_table(colorscale)
  • #7FC97F
  • #BEAED4
  • #FDC086
  • #FFFF99
  • #386CB0
  • #F0027F
  • #BF5B17
  • #666666