Colors in Visualization
Colors in Visualization
Data visualization is the graphical representation of data. One important step in data visualization process is mapping of numbers to colors. When mapping the numbers to colors, choosing the right color combinations is essential because charts that have colors that go well with each other make it easy to read and understand the data and the viewer can also easily perform the reverse mapping back to scalar values.
Basic Terms
Palette: In computer graphics, a palette is the set of available colors.
Hue: Hues refer to the set of pure colors within a color space. Hue defines pure colors as one of the six Primary and Secondary colors.
Saturation: Saturation refers to the intensity of color in an image, that is the strength or weakness of a color.
Lightness: Lightness of a color specifies how “bright” the color should be. 0% means the brightness is 0 and the color is black. 100% means maximum brightness and the color is white.
Color wheel: A color wheel is a visual organizational tool of color hues around a circle that help make the basic categories of color easier to understand. It shows the relationships between primary colors, secondary colors, and tertiary colors. It can be used as a point of reference when creating customized palettes.
Color model: Color models provide various methods to define colors, each model defining colors through the use of specific color components.

Source : http://warrenmars.com/visual_art/theory/colour_wheel/martian_colour_wheel_24_hue_r.png
color_palette() function
The color_palette function in Seaborn returns a list of colors defined in a color palette and these colors can be used in a plot. When the function is called without passing any arguments, it returns the current Matplotlib color cycle. The function can also be used in a with statement to temporarily set the color cycle for a plot or set of plots. The colors defined in a color palette can be displayed using the palplot() function, which plots the color palette in a horizontal array.
import seaborn as sns
current_palette = sns.color_palette() ## The color_palette function returns a list of RGB tuples.
current_palette
[(0.4541176470588235, 0.7066666666666666, 0.6270588235294118),
(0.8976470588235295, 0.5929411764705883, 0.4749019607843137),
(0.5894117647058825, 0.6415686274509804, 0.7596078431372548),
(0.8511764705882354, 0.5958823529411763, 0.7523529411764707),
(0.632156862745098, 0.7694117647058825, 0.4070588235294117),
(0.8776470588235292, 0.7733333333333332, 0.30666666666666687)]
sns.palplot(sns.color_palette(current_palette))
## palplot() function displays the colors in the current matplotlib color cycle.

The type of color palette to use in a visualization depends on the nature of the input data. Primarily three types of color palettes exist for data visualization:
- Qualitative palettes
- Sequential palettes
- Diverging palettes
Let us discuss about these palettes.
Qualitative color palettes
Qualitative or Categorical palettes can be used to represent categorical data, where there is no particular ordering of categories. Each color in the palette represents a distinct category. If the number of categories in the input data is more than the number of colors in the palette, the same set of colors will be looped over.
There are six variations of the default color cycle – deep, muted, pastel, bright, dark, and colorblind. These six palettes have varying degrees of saturation and luminance.
sns.palplot(sns.color_palette(palette='deep'))

sns.palplot(sns.color_palette(palette='muted'))

sns.palplot(sns.color_palette(palette='pastel'))

Circular color systems
hsl color model
HSL is short form for Hue, Saturation and Lightness. The HSL color model defines a given color according to its hue, saturation and lightness components.
The hls_palette function in Seaborn can be used to create a palette with evenly spaced colors in HLS hue space. The values for the parameters h, l, and s ranges between 0 and 1. By default, 6 colors in the hsl color model will be returned.
The hls_pallete() function below returns evenly spaced colors beginning from hue=0(red), keeping the saturation and lightness at 0.5.
sns.palplot(sns.hls_palette(n_colors=6, h=0, l=0.5, s=0.5))

The function below returns fully saturated version of same set of colors as above by setting s=1.
sns.palplot(sns.hls_palette(n_colors=6, h=0.5, l=0.3, s=1))

Look at the colors below, the brightness of these colors is 0.8.
sns.palplot(sns.hls_palette(n_colors=10, h=0.2, l=0.8, s=0.65))

husl color model
HSLuv color space has perceptually uniform lightness, which means every color with the same lightness value is perceived as equally bright by humans. HSLuv color space, is a human-friendly alternative to the HSL color space. The husl_palette function in Seaborn can be used to create a palette with evenly spaced colors in HUSL hue space. The values for the parameters h, l, and s ranges between 0 and 1. By default, 6 colors in the husl color model will be returned.
In the below example, the husl_palette returns colors beginning from h=0, with the saturation and luminosity set at 0.6.
sns.palplot(sns.husl_palette(s=0.6,l=0.6))

Darker shades of the above set of colors are displayed below by setting luminosity to 0.3
sns.palplot(sns.husl_palette(s=0.7,l=0.3))

Categorical Color Brewer palettes
There are many tools available online that help us to choose and create color palettes that best suit our needs. ColorBrewer is one such tool that offers a number of interesting color palettes of each type.
The choose_colorbrewer_palette() function in Seaborn, helps us to choose palettes from the Color Brewer library. This function, which must be used in a Jupyter notebook, launches an interactive Ipython widget function to choose and customize a color palette. To the choose_colorbrewer_palette() function we need to pass the type of color palette that we want to visualize. Using the interactive widget we can browse through various palettes available and adjust the saturation parameter.
This function, by default, returns the list of colors defined in a color palette, but if you need a Matplotlib colormap that can be used with Matplotlib plotting functions you can set the argument as_cmap to True.
sns.choose_colorbrewer_palette(data_type='s')
interactive(children=(Dropdown(description='name', options=('Greys', 'Reds', 'Greens', 'Blues', 'Oranges', 'Pu…
[(0.9575547866205305, 0.9575547866205305, 0.9575547866205305),
(0.9012072279892349, 0.9012072279892349, 0.9012072279892349),
(0.8328950403690888, 0.8328950403690888, 0.8328950403690888),
(0.7502191464821223, 0.7502191464821223, 0.7502191464821223),
(0.6434140715109573, 0.6434140715109573, 0.6434140715109573),
(0.5387158785082661, 0.5387158785082661, 0.5387158785082661),
(0.440322952710496, 0.440322952710496, 0.440322952710496),
(0.342883506343714, 0.342883506343714, 0.342883506343714),
(0.22329873125720878, 0.22329873125720878, 0.22329873125720878),
(0.10469819300269129, 0.10469819300269129, 0.10469819300269129)]
Sequential color palettes
Sequential color palettes are best to visualize numeric data that progresses from low to high (or vice versa) with light to dark colors. You can use a sequential color palette with a single hue, while changing the saturation and lightness. This allows us to focus our attention on data that have larger values(bright colors). Examples of single hue color palettes are Blues,Greens,Greys,Oranges.
sns.palplot(sns.color_palette('Blues'))

You can also reverse the order of colors in the palette by adding a suffix ‘_r’ to the palette name.
sns.palplot(sns.color_palette('Blues_r'))

sns.palplot(sns.color_palette('Greens_r'))

If you want darker shades of these colors , then you can add a suffix ‘_d’ to the palette name.
sns.palplot(sns.color_palette('Greens_d'))

Multi-hue sequential color palettes provide a better color contrast so that the colors are easier to differentiate. When multiple hues are used it is good to choose colors that are next to each other on the color wheel because they blend well together, the parameters saturation and lightness are to be adjusted accordingly.
sns.palplot(sns.color_palette('BuGn'))

sns.palplot(sns.color_palette('PuBuGn'))

Cubehelix palette
The Cubehelix system was designed to produce attractive palettes with a huge choice of hue, saturation and brightness. Images using this palette will look monotonically increasing to both the human eye and when printed in black and white.
The cubehelix palettes are a form of the sequential color palettes in which the hue is slightly changed and the brightness is increased linearly.
As shown below, you can pass the palette name and the number of colors ‘n’ to the color_palette() function to display……
sns.palplot(sns.color_palette("cubehelix", 8))

You can call the cubehelix_palette() function through the cubehelix interface provided by Seaborn. This function can be used to generate a sequential palette.
sns.palplot(sns.cubehelix_palette())

Parameters:
- start — hue to start from
- rotation — number of rotations the helix makes
- hue — saturation of the color
- gamma — Gamma factor to emphasize low or high intensity values
- light — Intensity of the lightest color in the palette
- dark – Intensity of the darkest color in the palette
- as_cmap – If True, return a matplotlib colormap instead of a list of colors
sns.palplot(sns.cubehelix_palette(start=0,rot=2,hue=0.5,gamma=0.3))

sns.palplot(sns.cubehelix_palette(start=0.5,rot=3,hue=1,gamma=1,dark=1,light=0))

It is also possible to generate a cubehelix palette in seaborn using a string-shorthand as shown below:
sns.palplot(sns.color_palette("ch:1.5,r=.4,l=.6"))

Custom sequential palettes
Seaborn comes up with custom sequential palettes such as the light_palette and dark_palette.You can generate a palette using the light_palette function by choosing a base color which blends from light to dark shade. You can use the interactive Ipython widget with choose_light_palette() function to select light palettes.
sns.palplot(sns.light_palette(color='#800080',n_colors=7,reverse=False,input='RGB'))

Parameters: color – HEX code, HTML color name of the chosen color reverse – If True, the order of colours is reversed as_cmap – If True, return a matplotlib colormap instead of a list of colors input – color space to which color values refer : {‘rgb’, ‘hls’, ‘husl’, xkcd’}
Using the dark_palette function, you can generate a palette by choosing a base color which blends from dark shade to color. You can use the interactive Ipython widget with choose_dark_palette() function to select dark palettes.
sns.palplot(sns.dark_palette(color='#b60c26',n_colors=6,reverse=False,input='hsl'))

Diverging color palettes
Diverging color schemes are best used to highlight both high and low extreme values. This color scheme is usually used for data that has a natural mid-point such as a zero. A diverging scheme shows all values below the mid-point as a sequential color scheme using one hue, and all values above the mid-point in a sequential scheme using a different hue. The hue at one extreme gradually tones down to a neutral color at the midpoint, and then the saturation of the second hue gradually increases to the other extreme.
Below we see few diverging palattes.
sns.palplot(sns.color_palette('RdBu',7))

sns.palplot(sns.color_palette('BrBG',7))

Custom diverging palettes
You can generate custom diverging palettes using the diverging_palette() function.
sns.palplot(sns.diverging_palette(h_neg=260,h_pos=350,n=7,s=75,l=50,sep=10,center='light'))

Parameters:
- h_neg,h_pos- hues for the left and right extremes of the palette, range between 0-359.
- s – saturation for both the extremes
- l – lightness for both the extremes
- sep – size of the intermediate region
- center – the center of the palette can be ‘light’ or ‘dark’
The set_palette function is used to set the matplotlib color cycle using a seaborn palette.
sns.set_palette(palette=sns.color_palette('Set2'), n_colors=6, desat=0.7, color_codes=False)
from matplotlib import pyplot as plt
tips = sns.load_dataset('tips')
sns.barplot('sex','tip',data=tips)
plt.show()
