{ "metadata": { "name": "", "signature": "sha256:6c5b87c17222dae4463d8eb6717de72da45b2479dc0bf363c8532410d989679f" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Cufflinks Colors\n", "\n", "Cufflinks also provides a wide set of tools for color managements; including color conversion across multiple spectrums and color table generation." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import cufflinks as cl" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Colors can be represented as strings:\n", "\n", "**HEX** `\"#db4052\"` \n", "**RGB** `\"rgb(219, 64, 82)\"` \n", "**RGBA** `\"rgba(219, 64, 82, 1.0)\"` \n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Color Conversions" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# The colors module includes a pre-defined set of commonly used colors\n", "red=cl.red\n", "red" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "'#db4052'" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "# HEX to RGB\n", "cl.hex_to_rgb(red)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "'rgb(219, 64, 82)'" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "# RGB to HEX\n", "cl.rgb_to_hex('rgb(219, 64, 82)')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "'#db4052'" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "# RGB or HEX to RGBA (transparency)\n", "cl.to_rgba('#3780bf',.5), cl.to_rgba('rgb(219, 64, 82)',.4)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "('rgba(55, 128, 191, 0.5)', 'rgba(219, 64, 82, 0.4)')" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "# RGBA to RGB (flatten transparency)\n", "# By default assumes that the transparency color is *white*, however this can be also passed as a parameter. \n", "bg=cl.white\n", "cl.rgba_to_rgb('rgba(219, 64, 82, 0.4)',bg)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "'rgb(240, 178, 185)'" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Normalization" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Cufflinks.colors.normalize will always return the an hex value for all types of colors\n", "colors=['#f08','rgb(240, 178, 185)','rgba(219, 64, 82, 0.4)',cl.green]\n", "[cl.normalize(c) for c in colors]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "['#ff0088', '#f0b2b9', '#f0b2b9', '#32ab60']" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Color Ranges\n", "\n", "A range of colors can be generated using a base color and varying the saturation." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# 10 different tones of pink\n", "cl.color_range(cl.pink,10)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ "['#000000',\n", " '#33001b',\n", " '#660036',\n", " '#990051',\n", " '#cc006c',\n", " '#ff0088',\n", " '#ff329f',\n", " '#ff65b7',\n", " '#ff99cf',\n", " '#ffcce7',\n", " '#ffffff']" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Color Tables\n", "\n", "This function is meant to be used in an **iPython Notebook**.\n", "It generates an HTML table to display either a defined list of colors or to automatically generate a range of colors." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Displaying a table of defined colors (list)\n", "colors=['#f08', 'rgb(240, 178, 185)', cl.blue , '#32ab60']\n", "cl.color_table(colors)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 23, "text": [ "" ] } ], "prompt_number": 23 }, { "cell_type": "code", "collapsed": false, "input": [ "# Generating 15 shades of orange\n", "cl.color_table(cl.orange,15)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 25, "text": [ "" ] } ], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Color Generators\n", "\n", "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. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a generator using 3 defined base colors\n", "colors=[cl.green,cl.orange,cl.blue]\n", "gen=cl.colorgen(colors)\n", "outputColors=[gen.next() for _ in range(15)]\n", "cl.color_table(outputColors)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 34, "text": [ "" ] } ], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "# Create a generator with default set of colors\n", "gen=cl.colorgen()\n", "outputColors=[gen.next() for _ in range(15)]\n", "cl.color_table(outputColors)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
  • \n", "\t\t#84cc9f\n", "\t\t
  • \n", "\t\t#87b2d8\n", "\t\t
  • \n", "\t\t#ffc184\n", "\t\t
  • \n", "\t\t#e98c97\n", "\t\t
  • \n", "\t\t#a284cc\n", "\t\t
  • \n", "\t\t#835abb\n", "\t\t
  • \n", "\t\t#5abb7f\n", "\t\t
  • \n", "\t\t#5e99cb\n", "\t\t
  • \n", "\t\t#ffad5b\n", "\t\t
  • \n", "\t\t#e26674\n", "\t\t
  • \n", "\t\t#db4052\n", "\t\t
  • \n", "\t\t#6432ab\n", "\t\t
  • \n", "\t\t#32ab60\n", "\t\t
  • \n", "\t\t#3780bf\n", "\t\t
  • \n", "\t\t#ff9933\n", "\t\t
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 33, "text": [ "" ] } ], "prompt_number": 33 } ], "metadata": {} } ] }