matplotlib colors

matplotlib.colors

A class for converting color arguments to RGB or RGBA

This class instantiates a single instance colorConverter that is used to convert matlab color strings to RGB. RGB is a tuple of float RGB values in the range 0-1.

Commands which take color arguments can use several formats to specify the colors. For the basic builtin colors, you can use a single letter

b : blue g : green r : red c : cyan m : magenta y : yellow k : black w : white

Gray shades can be given as a string encoding a float in the 0-1 range, e.g.:

color = '0.75'

For a greater range of colors, you have two options. You can specify the color using an html hex string, as in:

color = '#eeefff'

or you can pass an R , G , B tuple, where each of R , G , B are in the range [0,1].

Finally, legal html names for colors, like ‘red’, ‘burlywood’ and ‘chartreuse’ are supported.

class matplotlib.colors.BoundaryNorm(boundaries, ncolors, clip=False)

Bases: matplotlib.colors.Normalize

Generate a colormap index based on discrete intervals.

Unlike Normalize or LogNorm, BoundaryNorm maps values to integers instead of to the interval 0-1.

Mapping to the 0-1 interval could have been done via piece-wise linear interpolation, but using integers seems simpler, and reduces the number of conversions back and forth between integer and floating point.

boundaries
a monotonically increasing sequence
ncolors
number of colors in the colormap to be used

If:

b[i] <= v < b[i+1]

then v is mapped to color j; as i varies from 0 to len(boundaries)-2, j goes from 0 to ncolors-1.

Out-of-range values are mapped to -1 if low and ncolors if high; these are converted to valid indices by Colormap.__call__() .

inverse(value)
class matplotlib.colors.ColorConverter
to_rgb(arg)

Returns an RGB tuple of three floats from 0-1.

arg can be an RGB or RGBA sequence or a string in any of several forms:

  1. a letter from the set ‘rgbcmykw’
  2. a hex color string, like ‘#00FFFF’
  3. a standard name, like ‘aqua’
  4. a float, like ‘0.4’, indicating gray on a 0-1 scale

if arg is RGBA, the A will simply be discarded.

to_rgba(arg, alpha=None)

Returns an RGBA tuple of four floats from 0-1.

For acceptable values of arg, see to_rgb(). If arg is an RGBA sequence and alpha is not None, alpha will replace the original A.

to_rgba_array(c, alpha=None)

Returns an Numpy array of RGBA tuples.

Accepts a single mpl color spec or a sequence of specs. If the sequence is a list or array, the items are changed in place, but an array copy is still returned.

Special case to handle “no color”: if c is “none” (case-insensitive), then an empty array will be returned. Same for an empty list.

class matplotlib.colors.Colormap(name, N=256)

Base class for all scalar to rgb mappings

Important methods:

Public class attributes:
N : number of rgb quantization levels name : name of colormap
is_gray()
set_bad(color='k', alpha=1.0)
Set color to be used for masked values.
set_over(color='k', alpha=1.0)
Set color to be used for high out-of-range values. Requires norm.clip = False
set_under(color='k', alpha=1.0)
Set color to be used for low out-of-range values. Requires norm.clip = False
class matplotlib.colors.LinearSegmentedColormap(name, segmentdata, N=256)

Bases: matplotlib.colors.Colormap

Colormap objects based on lookup tables using linear segments.

The lookup transfer function is a simple linear function between defined intensities. There is no limit to the number of segments that may be defined. Though as the segment intervals start containing fewer and fewer array locations, there will be inevitable quantization errors

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. See makeMappingArray for details
class matplotlib.colors.ListedColormap(colors, name='from_list', N=None)

Bases: matplotlib.colors.Colormap

Colormap object generated from a list of colors.

This may be most useful when indexing directly into a colormap, but it can also be used to generate special colormaps for ordinary mapping.

Make a colormap from a list of colors.

colors
a list of matplotlib color specifications, or an equivalent Nx3 floating point array (N rgb values)
name
a string to identify the colormap
N

the number of entries in the map. The default is None, in which case there is one colormap entry for each element in the list of colors. If:

N < len(colors)

the list will be truncated at N. If:

N > len(colors)

the list will be extended by repetition.

class matplotlib.colors.LogNorm(vmin=None, vmax=None, clip=False)

Bases: matplotlib.colors.Normalize

Normalize a given value to the 0-1 range on a log scale

If vmin or vmax is not given, they are taken from the input’s minimum and maximum value respectively. If clip is True and the given value falls outside the range, the returned value will be 0 or 1, whichever is closer. Returns 0 if:

vmin==vmax

Works with scalars or arrays, including masked arrays. If clip is True, masked values are set to 1; otherwise they remain masked. Clipping silently defeats the purpose of setting the over, under, and masked colors in the colormap, so it is likely to lead to surprises; therefore the default is clip = False.

inverse(value)
class matplotlib.colors.NoNorm(vmin=None, vmax=None, clip=False)

Bases: matplotlib.colors.Normalize

Dummy replacement for Normalize, for the case where we want to use indices directly in a ScalarMappable .

If vmin or vmax is not given, they are taken from the input’s minimum and maximum value respectively. If clip is True and the given value falls outside the range, the returned value will be 0 or 1, whichever is closer. Returns 0 if:

vmin==vmax

Works with scalars or arrays, including masked arrays. If clip is True, masked values are set to 1; otherwise they remain masked. Clipping silently defeats the purpose of setting the over, under, and masked colors in the colormap, so it is likely to lead to surprises; therefore the default is clip = False.

inverse(value)
class matplotlib.colors.Normalize(vmin=None, vmax=None, clip=False)

Normalize a given value to the 0-1 range

If vmin or vmax is not given, they are taken from the input’s minimum and maximum value respectively. If clip is True and the given value falls outside the range, the returned value will be 0 or 1, whichever is closer. Returns 0 if:

vmin==vmax

Works with scalars or arrays, including masked arrays. If clip is True, masked values are set to 1; otherwise they remain masked. Clipping silently defeats the purpose of setting the over, under, and masked colors in the colormap, so it is likely to lead to surprises; therefore the default is clip = False.

autoscale(A)
Set vmin, vmax to min, max of A.
autoscale_None(A)
autoscale only None-valued vmin or vmax
inverse(value)
scaled()
return true if vmin and vmax set
matplotlib.colors.hex2color(s)
Take a hex string s and return the corresponding rgb 3-tuple Example: #efefef -> (0.93725, 0.93725, 0.93725)
matplotlib.colors.is_color_like(c)
matplotlib.colors.makeMappingArray(N, data)

Create an N -element 1-d lookup table

data represented by a list of x,y0,y1 mapping correspondences. Each element in this list represents how a value between 0 and 1 (inclusive) represented by x is mapped to a corresponding value between 0 and 1 (inclusive). The two values of y are to allow for discontinuous mapping functions (say as might be found in a sawtooth) where y0 represents the value of y for values of x <= to that given, and y1 is the value to be used for x > than that given). The list must start with x=0, end with x=1, and all values of x must be in increasing order. Values between the given mapping points are determined by simple linear interpolation.

The function returns an array “result” where result[x*(N-1)] gives the closest value for values of x between 0 and 1.

class matplotlib.colors.no_norm(vmin=None, vmax=None, clip=False)

Bases: matplotlib.colors.Normalize

Dummy replacement for Normalize, for the case where we want to use indices directly in a ScalarMappable .

If vmin or vmax is not given, they are taken from the input’s minimum and maximum value respectively. If clip is True and the given value falls outside the range, the returned value will be 0 or 1, whichever is closer. Returns 0 if:

vmin==vmax

Works with scalars or arrays, including masked arrays. If clip is True, masked values are set to 1; otherwise they remain masked. Clipping silently defeats the purpose of setting the over, under, and masked colors in the colormap, so it is likely to lead to surprises; therefore the default is clip = False.

inverse(value)
class matplotlib.colors.normalize(vmin=None, vmax=None, clip=False)

Normalize a given value to the 0-1 range

If vmin or vmax is not given, they are taken from the input’s minimum and maximum value respectively. If clip is True and the given value falls outside the range, the returned value will be 0 or 1, whichever is closer. Returns 0 if:

vmin==vmax

Works with scalars or arrays, including masked arrays. If clip is True, masked values are set to 1; otherwise they remain masked. Clipping silently defeats the purpose of setting the over, under, and masked colors in the colormap, so it is likely to lead to surprises; therefore the default is clip = False.

autoscale(A)
Set vmin, vmax to min, max of A.
autoscale_None(A)
autoscale only None-valued vmin or vmax
inverse(value)
scaled()
return true if vmin and vmax set
matplotlib.colors.rgb2hex(rgb)
Given a len 3 rgb tuple of 0-1 floats, return the hex string

Table Of Contents

Previous topic

matplotlib colorbar

Next topic

matplotlib pyplot

This Page

Quick search