palettes - Mapping values onto the domain of a scale

Palettes are the link between data values and the values along the dimension of a scale. Before a collection of values can be represented on a scale, they are transformed by a palette. This transformation is knowing as mapping. Values are mapped onto a scale by a palette.

Scales tend to have restrictions on the magnitude of quantities that they can intelligibly represent. For example, the size of a point should be significantly smaller than the plot panel onto which it is plotted or else it would be hard to compare two or more points. Therefore palettes must be created that enforce such restrictions. This is the reason for the *_pal functions that create and return the actual palette functions.

mizani.palettes.hls_palette(n_colors=6, h=0.01, l=0.6, s=0.65)[source]

Get a set of evenly spaced colors in HLS hue space.

h, l, and s should be between 0 and 1

Parameters
n_colorspython:int

number of colors in the palette

hpython:float

first hue

lpython:float

lightness

spython:float

saturation

Returns
palettepython:list

List of colors as RGB hex strings.

See also

husl_palette

Make a palette using evenly spaced circular hues in the HUSL system.

Examples

>>> len(hls_palette(2))
2
>>> len(hls_palette(9))
9
mizani.palettes.husl_palette(n_colors=6, h=0.01, s=0.9, l=0.65)[source]

Get a set of evenly spaced colors in HUSL hue space.

h, s, and l should be between 0 and 1

Parameters
n_colorspython:int

number of colors in the palette

hpython:float

first hue

spython:float

saturation

lpython:float

lightness

Returns
palettepython:list

List of colors as RGB hex strings.

See also

hls_palette

Make a palette using evenly spaced circular hues in the HSL system.

Examples

>>> len(husl_palette(3))
3
>>> len(husl_palette(11))
11
mizani.palettes.rescale_pal(range=(0.1, 1))[source]

Rescale the input to the specific output range.

Useful for alpha, size, and continuous position.

Parameters
rangepython:tuple

Range of the scale

Returns
outfunction

Palette function that takes a sequence of values in the range [0, 1] and returns values in the specified range.

Examples

>>> palette = rescale_pal()
>>> palette([0, .2, .4, .6, .8, 1])
array([0.1 , 0.28, 0.46, 0.64, 0.82, 1.  ])

The returned palette expects inputs in the [0, 1] range. Any value outside those limits is clipped to range[0] or range[1].

>>> palette([-2, -1, 0.2, .4, .8, 2, 3])
array([0.1 , 0.1 , 0.28, 0.46, 0.82, 1.  , 1.  ])
mizani.palettes.area_pal(range=(1, 6))[source]

Point area palette (continuous).

Parameters
rangepython:tuple

Numeric vector of length two, giving range of possible sizes. Should be greater than 0.

Returns
outfunction

Palette function that takes a sequence of values in the range [0, 1] and returns values in the specified range.

Examples

>>> x = np.arange(0, .6, .1)**2
>>> palette = area_pal()
>>> palette(x)
array([1. , 1.5, 2. , 2.5, 3. , 3.5])

The results are equidistant because the input x is in area space, i.e it is squared.

mizani.palettes.abs_area(max)[source]

Point area palette (continuous), with area proportional to value.

Parameters
maxpython:float

A number representing the maximum size

Returns
outfunction

Palette function that takes a sequence of values in the range [0, 1] and returns values in the range [0, max].

Examples

>>> x = np.arange(0, .8, .1)**2
>>> palette = abs_area(5)
>>> palette(x)
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5])

Compared to area_pal(), abs_area() will handle values in the range [-1, 0] without returning np.nan. And values whose absolute value is greater than 1 will be clipped to the maximum.

mizani.palettes.grey_pal(start=0.2, end=0.8)[source]

Utility for creating continuous grey scale palette

Parameters
startpython:float

grey value at low end of palette

endpython:float

grey value at high end of palette

Returns
outfunction

Continuous color palette that takes a single int parameter n and returns n equally spaced colors.

Examples

>>> palette = grey_pal()
>>> palette(5)
['#333333', '#737373', '#989898', '#b5b5b5', '#cccccc']
mizani.palettes.hue_pal(h=0.01, l=0.6, s=0.65, color_space='hls')[source]

Utility for making hue palettes for color schemes.

Parameters
hpython:float

first hue. In the [0, 1] range

lpython:float

lightness. In the [0, 1] range

spython:float

saturation. In the [0, 1] range

color_space'hls' | 'husl'

Color space to use for the palette

Returns
outfunction

A discrete color palette that takes a single int parameter n and returns n equally spaced colors. Though the palette is continuous, since it is varies the hue it is good for categorical data. However if n is large enough the colors show continuity.

Examples

>>> hue_pal()(5)
['#db5f57', '#b9db57', '#57db94', '#5784db', '#c957db']
>>> hue_pal(color_space='husl')(5)
['#e0697e', '#9b9054', '#569d79', '#5b98ab', '#b675d7']
mizani.palettes.brewer_pal(type='seq', palette=1, direction=1)[source]

Utility for making a brewer palette

Parameters
type'sequential' | 'qualitative' | 'diverging'

Type of palette. Sequential, Qualitative or Diverging. The following abbreviations may be used, seq, qual or div.

palettepython:int | python:str

Which palette to choose from. If is an integer, it must be in the range [0, m], where m depends on the number sequential, qualitative or diverging palettes. If it is a string, then it is the name of the palette.

directionpython:int

The order of colours in the scale. If -1 the order of colors is reversed. The default is 1.

Returns
outfunction

A color palette that takes a single int parameter n and returns n colors. The maximum value of n varies depending on the parameters.

Examples

>>> brewer_pal()(5)
['#EFF3FF', '#BDD7E7', '#6BAED6', '#3182BD', '#08519C']
>>> brewer_pal('qual')(5)
['#7FC97F', '#BEAED4', '#FDC086', '#FFFF99', '#386CB0']
>>> brewer_pal('qual', 2)(5)
['#1B9E77', '#D95F02', '#7570B3', '#E7298A', '#66A61E']
>>> brewer_pal('seq', 'PuBuGn')(5)
['#F6EFF7', '#BDC9E1', '#67A9CF', '#1C9099', '#016C59']

The available color names for each palette type can be obtained using the following code:

import palettable.colorbrewer as brewer

print([k for k in brewer.COLOR_MAPS['Sequential'].keys()])
print([k for k in brewer.COLOR_MAPS['Qualitative'].keys()])
print([k for k in brewer.COLOR_MAPS['Diverging'].keys()])
mizani.palettes.gradient_n_pal(colors, values=None, name='gradientn')[source]

Create a n color gradient palette

Parameters
colorspython:list

list of colors

valuespython:list, optional

list of points in the range [0, 1] at which to place each color. Must be the same size as colors. Default to evenly space the colors

namepython:str

Name to call the resultant MPL colormap

Returns
outfunction

Continuous color palette that takes a single parameter either a float or a sequence of floats maps those value(s) onto the palette and returns color(s). The float(s) must be in the range [0, 1].

Examples

>>> palette = gradient_n_pal(['red', 'blue'])
>>> palette([0, .25, .5, .75, 1])
['#ff0000', '#bf0040', '#7f0080', '#3f00c0', '#0000ff']
>>> palette([-np.inf, 0, np.nan, 1, np.inf])
[nan, '#ff0000', nan, '#0000ff', nan]
mizani.palettes.cmap_pal(name, lut=None)[source]

Create a continuous palette using an MPL colormap

Parameters
namepython:str

Name of colormap

lutpython:None | python:int

This is the number of entries desired in the lookup table. Default is None, leave it up Matplotlib.

Returns
outfunction

Continuous color palette that takes a single parameter either a float or a sequence of floats maps those value(s) onto the palette and returns color(s). The float(s) must be in the range [0, 1].

Examples

>>> palette = cmap_pal('viridis')
>>> palette([.1, .2, .3, .4, .5])
['#482475', '#414487', '#355f8d', '#2a788e', '#21918c']
mizani.palettes.cmap_d_pal(name, lut=None)[source]

Create a discrete palette using an MPL Listed colormap

Parameters
namepython:str

Name of colormap

lutpython:None | python:int

This is the number of entries desired in the lookup table. Default is None, leave it up Matplotlib.

Returns
outfunction

A discrete color palette that takes a single int parameter n and returns n colors. The maximum value of n varies depending on the parameters.

Examples

>>> palette = cmap_d_pal('viridis')
>>> palette(5)
['#440154', '#3b528b', '#21918c', '#5cc863', '#fde725']
mizani.palettes.desaturate_pal(color, prop, reverse=False)[source]

Create a palette that desaturate a color by some proportion

Parameters
colormatplotlib color

hex, rgb-tuple, or html color name

proppython:float

saturation channel of color will be multiplied by this value

reversebool

Whether to reverse the palette.

Returns
outfunction

Continuous color palette that takes a single parameter either a float or a sequence of floats maps those value(s) onto the palette and returns color(s). The float(s) must be in the range [0, 1].

Examples

>>> palette = desaturate_pal('red', .1)
>>> palette([0, .25, .5, .75, 1])
['#ff0000', '#e21d1d', '#c53a3a', '#a95656', '#8c7373']
mizani.palettes.manual_pal(values)[source]

Create a palette from a list of values

Parameters
valuespython:sequence

Values that will be returned by the palette function.

Returns
outfunction

A function palette that takes a single int parameter n and returns n values.

Examples

>>> palette = manual_pal(['a', 'b', 'c', 'd', 'e'])
>>> palette(3)
['a', 'b', 'c']
mizani.palettes.xkcd_palette(colors)[source]

Make a palette with color names from the xkcd color survey.

See xkcd for the full list of colors: http://xkcd.com/color/rgb/

Parameters
colorspython:list of strings

List of keys in the mizani.external.xkcd_rgb dictionary.

Returns
palettepython:list

List of colors as RGB hex strings.

Examples

>>> palette = xkcd_palette(['red', 'green', 'blue'])
>>> palette
['#e50000', '#15b01a', '#0343df']
>>> from mizani.external import xkcd_rgb
>>> list(sorted(xkcd_rgb.keys()))[:5]
['acid green', 'adobe', 'algae', 'algae green', 'almost black']
mizani.palettes.crayon_palette(colors)[source]

Make a palette with color names from Crayola crayons.

The colors come from http://en.wikipedia.org/wiki/List_of_Crayola_crayon_colors

Parameters
colorspython:list of strings

List of keys in the mizani.external.crayloax_rgb dictionary.

Returns
palettepython:list

List of colors as RGB hex strings.

Examples

>>> palette = crayon_palette(['almond', 'silver', 'yellow'])
>>> palette
['#eed9c4', '#c9c0bb', '#fbe870']
>>> from mizani.external import crayon_rgb
>>> list(sorted(crayon_rgb.keys()))[:5]
['almond', 'antique brass', 'apricot', 'aquamarine', 'asparagus']
mizani.palettes.cubehelix_pal(start=0, rot=0.4, gamma=1.0, hue=0.8, light=0.85, dark=0.15, reverse=False)[source]

Utility for creating continuous palette from the cubehelix system.

This produces a colormap with linearly-decreasing (or increasing) brightness. That means that information will be preserved if printed to black and white or viewed by someone who is colorblind.

Parameters
startpython:float (0 <= start <= 3)

The hue at the start of the helix.

rotpython:float

Rotations around the hue wheel over the range of the palette.

gammapython:float (0 <= gamma)

Gamma factor to emphasize darker (gamma < 1) or lighter (gamma > 1) colors.

huepython:float (0 <= hue <= 1)

Saturation of the colors.

darkpython:float (0 <= dark <= 1)

Intensity of the darkest color in the palette.

lightpython:float (0 <= light <= 1)

Intensity of the lightest color in the palette.

reversebool

If True, the palette will go from dark to light.

Returns
outfunction

Continuous color palette that takes a single int parameter n and returns n equally spaced colors.

References

Green, D. A. (2011). "A colour scheme for the display of astronomical intensity images". Bulletin of the Astromical Society of India, Vol. 39, p. 289-295.

Examples

>>> palette = cubehelix_pal()
>>> palette(5)
['#edd1cb', '#d499a7', '#aa688f', '#6e4071', '#2d1e3e']