Working with colors in Python is confusing:
colorsys
requires 0-1 decimal RGB / HSL / HSV values, and works only with single colorsmatplotlib
is heavy if you only want color conversion and interpolation- Cynthia Brewer’s popular color scales aren’t easily available through a pip library
- Colors are often not displayed in formats that work with modern css
- In IPython notebook, there are no intuitive, lightweight tools to quickly inspect color scales
… Introducing colorlover
, a web-friendly color conversion and interpolation library.
To install:
sudo pip install colorlover
Here’s an example of using colorlover
in IPython notebook:
import colorlover as cl
from IPython.display import HTML
HTML(cl.to_html( cl.flipper()['seq']['3'] ))
Colors are displayed in RGB string format by default:
ryb = cl.scales['3']['div']['RdYlBu']; ryb
['rgb(252,141,89)', 'rgb(255,255,191)', 'rgb(145,191,219)']
And its easy to change to other color models:
cl.to_hsl( ryb )
['hsl(19.0, 96.0%, 67.0%)', 'hsl(60.0, 100.0%, 87.0%)', 'hsl(203.0, 51.0%, 71.0%)']
colorlover
is ideal for charting with Plotly:import plotly.plotly as py
from plotly.graph_objs import *
import math
un='IPython.Demo'; k='1fw3zw2o13'; py.sign_in(un,k);
bupu = cl.scales['9']['seq']['BuPu']
bupu500 = cl.interp( bupu, 500 ) # Map color scale to 500 bins
data = Data([ Scatter(
x = [ i * 0.1 for i in range(500) ],
y = [ math.sin(j * 0.1) for j in range(500) ],
mode='markers',
marker=Marker(color=bupu500,size=22.0,line=Line(color='black',width=2)),
text = cl.to_rgb( bupu500 ),
opacity = 0.7
)])
layout = Layout( showlegend=False, \
xaxis=XAxis(zeroline=False), yaxis=YAxis(zeroline=False) )
fig = Figure(data=data, layout=layout)
py.iplot(fig, filename='spectral_bubblechart')
See this IPython notebook for full usage.
Documentation and code is hosted on GitHub.
Inspiration and color scales in cl.scales
from Cythnia Brewer’s ColorBrewer.