Module: color

Inheritance diagram for mpltools.color:

LinearColormap

class mpltools.color.LinearColormap(name, color_data, index=None, **kwargs)

Bases: matplotlib.colors.LinearSegmentedColormap

Create Matplotlib colormap with color values specified at key points.

This class simplifies the call signature of LinearSegmentedColormap. By default, colors specified by color_data are equally spaced along the colormap.

Parameters :

name : str

Name of colormap.

color_data : list or dict

Colors at each index value. Two input types are supported:

List of RGB or RGBA tuples. For example, red and blue:

color_data = [(1, 0, 0), (0, 0, 1)]

Dict of ‘red’, ‘green’, ‘blue’, and (optionally) ‘alpha’ values. For example, the following would give a red-to-blue gradient:

color_data = {'red': [1, 0], 'green': [0, 0], 'blue': [0, 1]}

index : list of floats (0, 1)

Note that these indices must match the length of color_data. If None, colors in color_data are equally spaced in colormap.

Examples

Linear colormap going from white to red

>>> white_red = LinearColormap('white_red', [(1, 1, 1), (0.8, 0, 0)])

Colormap going from blue to white to red

>>> bwr = LinearColormap('blue_white_red', [(0.0, 0.2, 0.4),    # blue
...                                         (1.0, 1.0, 1.0),    # white
...                                         (0.4, 0.0, 0.1)])   # red

You can use a repeated index to get a segmented color. - Blue below midpoint of colormap, red above mid point. - Alpha maximum at the edges, minimum in the middle.

>>> bcr_rgba = [(0.02, 0.2, 0.4, 1),    # grayish blue, opaque
...             (0.02, 0.2, 0.4, 0.3),  # grayish blue, transparent
...             (0.4,  0.0, 0.1, 0.3),  # dark red, transparent
...             (0.4,  0.0, 0.1, 1)]    # dark red, opaque
>>> blue_clear_red = color.LinearColormap('blue_clear_red', bcr_rgba,
...                                       index=[0, 0.5, 0.5, 1])

Methods

from_list(name, colors[, N, gamma]) Make a linear segmented colormap with name from a sequence of colors which evenly transitions from colors[0] at val=0 to colors[-1] at val=1.
is_gray()
set_bad([color, alpha]) Set color to be used for masked values.
set_gamma(gamma) Set a new gamma value and regenerate color map.
set_over([color, alpha]) Set color to be used for high out-of-range values.
set_under([color, alpha]) Set color to be used for low out-of-range values.
__init__(name, color_data, index=None, **kwargs)
static from_list(name, colors, N=256, gamma=1.0)

Make a linear segmented colormap with name from a sequence of colors which evenly transitions from colors[0] at val=0 to colors[-1] at val=1. N is the number of rgb quantization levels. Alternatively, a list of (value, color) tuples can be given to divide the range unevenly.

is_gray()
set_bad(color='k', alpha=None)

Set color to be used for masked values.

set_gamma(gamma)

Set a new gamma value and regenerate color map.

set_over(color='k', alpha=None)

Set color to be used for high out-of-range values. Requires norm.clip = False

set_under(color='k', alpha=None)

Set color to be used for low out-of-range values. Requires norm.clip = False

LinearSegmentedColormap

class mpltools.color.LinearSegmentedColormap(name, segmentdata, N=256, gamma=1.0)

Bases: matplotlib.colors.Colormap

Colormap objects based on lookup tables using linear segments.

The lookup table is generated using linear interpolation for each primary color, with the 0-1 domain divided into any number of segments.

Methods

from_list(name, colors[, N, gamma]) Make a linear segmented colormap with name from a sequence of colors which evenly transitions from colors[0] at val=0 to colors[-1] at val=1.
is_gray()
set_bad([color, alpha]) Set color to be used for masked values.
set_gamma(gamma) Set a new gamma value and regenerate color map.
set_over([color, alpha]) Set color to be used for high out-of-range values.
set_under([color, alpha]) Set color to be used for low out-of-range values.
__init__(name, segmentdata, N=256, gamma=1.0)

Create color map from linear mapping segments

segmentdata argument is a dictionary with a red, green and blue entries. Each entry should be a list of x, y0, y1 tuples, forming rows in a table. Entries for alpha are optional.

Example: suppose you want red to increase from 0 to 1 over the bottom half, green to do the same over the middle half, and blue over the top half. Then you would use:

cdict = {'red':   [(0.0,  0.0, 0.0),
                   (0.5,  1.0, 1.0),
                   (1.0,  1.0, 1.0)],

         'green': [(0.0,  0.0, 0.0),
                   (0.25, 0.0, 0.0),
                   (0.75, 1.0, 1.0),
                   (1.0,  1.0, 1.0)],

         'blue':  [(0.0,  0.0, 0.0),
                   (0.5,  0.0, 0.0),
                   (1.0,  1.0, 1.0)]}

Each row in the table for a given color is a sequence of x, y0, y1 tuples. In each sequence, x must increase monotonically from 0 to 1. For any input value z falling between x[i] and x[i+1], the output value of a given color will be linearly interpolated between y1[i] and y0[i+1]:

row i:   x  y0  y1
               /
              /
row i+1: x  y0  y1

Hence y0 in the first row and y1 in the last row are never used.

See also

LinearSegmentedColormap.from_list()
Static method; factory function for generating a smoothly-varying LinearSegmentedColormap.
makeMappingArray()
For information about making a mapping array.
static from_list(name, colors, N=256, gamma=1.0)

Make a linear segmented colormap with name from a sequence of colors which evenly transitions from colors[0] at val=0 to colors[-1] at val=1. N is the number of rgb quantization levels. Alternatively, a list of (value, color) tuples can be given to divide the range unevenly.

is_gray()
set_bad(color='k', alpha=None)

Set color to be used for masked values.

set_gamma(gamma)

Set a new gamma value and regenerate color map.

set_over(color='k', alpha=None)

Set color to be used for high out-of-range values. Requires norm.clip = False

set_under(color='k', alpha=None)

Set color to be used for low out-of-range values. Requires norm.clip = False

mpltools.color.color_mapper(parameter_range) Return color mapper, which returns color based on parameter value.
mpltools.color.colors_from_cmap([length, ...]) Return color cycle from a given colormap.
mpltools.color.cycle_cmap([length, cmap, ...]) Set default color cycle of matplotlib based on colormap.
mpltools.color.rgb_list_to_colordict(rgb_list)

color_mapper

mpltools.color.color_mapper(parameter_range, cmap=None, start=None, stop=None)[source]

Return color mapper, which returns color based on parameter value.

Parameters :

parameter_range : tuple of floats

Minimum and maximum value of parameter.

cmap : str or colormap

A matplotlib colormap (see matplotlib.pyplot.cm) or the name of one.

start, stop: 0 <= float <= 1 :

Limit colormap to this range (start < stop 1). You should limit the range of colormaps with light values (assuming a white background).

Returns :

map_color : function

Function that returns an RGBA color from a parameter value.

colors_from_cmap

mpltools.color.colors_from_cmap(length=50, cmap=None, start=None, stop=None)[source]

Return color cycle from a given colormap.

Parameters :

length : int

The number of colors in the cycle. When length is large (> ~10), it is difficult to distinguish between successive lines because successive colors are very similar.

cmap : str

Name of a matplotlib colormap (see matplotlib.pyplot.cm).

start, stop: 0 <= float <= 1 :

Limit colormap to this range (start < stop 1). You should limit the range of colormaps with light values (assuming a white background). Some colors have default start/stop values (see CMAP_RANGE).

Returns :

colors : list

List of RGBA colors.

See also

cycle_cmap

cycle_cmap

mpltools.color.cycle_cmap(length=50, cmap=None, start=None, stop=None, ax=None)[source]

Set default color cycle of matplotlib based on colormap.

Note that the default color cycle is not changed if ax parameter is set; only the axes’s color cycle will be changed.

Parameters :

length : int

The number of colors in the cycle. When length is large (> ~10), it is difficult to distinguish between successive lines because successive colors are very similar.

cmap : str

Name of a matplotlib colormap (see matplotlib.pyplot.cm).

start, stop: 0 <= float <= 1 :

Limit colormap to this range (start < stop 1). You should limit the range of colormaps with light values (assuming a white background). Some colors have default start/stop values (see CMAP_RANGE).

ax : matplotlib axes

If ax is not None, then change the axes’s color cycle instead of the default color cycle.

rgb_list_to_colordict

mpltools.color.rgb_list_to_colordict(rgb_list)

Table Of Contents

Previous topic

Module: annotation