A module for converting numbers or color arguments to RGB or RGBA
RGB and RGBA are sequences of, respectively, 3 or 4 floats in the range 0-1.
This module includes functions and classes for color specification conversions, and for mapping numbers to colors in a 1-D array of colors called a colormap. Colormapping typically involves two steps: a data array is first mapped onto the range 0-1 using an instance of Normalize or of a subclass; then this number in the 0-1 range is mapped to a color using an instance of a subclass of Colormap. Two are provided here: LinearSegmentedColormap, which is used to generate all the built-in colormap instances, but is also useful for making custom colormaps, and ListedColormap, which is used for generating a custom colormap from a list of color specifications.
The module also provides a single instance, colorConverter, of the ColorConverter class providing methods for converting single color specifications or sequences of them to RGB or RGBA.
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.
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.
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__() .
Bases: object
Provides methods for converting color specifications to RGB or RGBA
Caching is used for more efficient conversion upon repeated calls with the same argument.
Ordinarily only the single instance instantiated in this module, colorConverter, is needed.
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:
- a letter from the set ‘rgbcmykw’
- a hex color string, like ‘#00FFFF’
- a standard name, like ‘aqua’
- a float, like ‘0.4’, indicating gray on a 0-1 scale
if arg is RGBA, the A will simply be discarded.
Returns an RGBA tuple of four floats from 0-1.
For acceptable values of arg, see to_rgb(). In addition, if arg is “none” (case-insensitive), then (0,0,0,0) will be returned. If arg is an RGBA sequence and alpha is not None, alpha will replace the original A.
Returns a numpy array of RGBA tuples.
Accepts a single mpl color spec or a sequence of specs.
Special case to handle “no color”: if c is “none” (case-insensitive), then an empty array will be returned. Same for an empty list.
Bases: object
Base class for all scalar to rgb mappings
Important methods:
Public class attributes: N : number of rgb quantization levels name : name of colormap
Set color to be used for masked values.
Set color to be used for high out-of-range values. Requires norm.clip = False
Set color to be used for low out-of-range values. Requires norm.clip = False
Bases: object
Create a light source coming from the specified azimuth and elevation. Angles are in degrees, with the azimuth measured clockwise from north and elevation up from the zero plane of the surface. The shade() is used to produce rgb values for a shaded relief image given a data array.
Specify the azimuth (measured clockwise from south) and altitude (measured up from the plane of the surface) of the light source in degrees.
The color of the resulting image will be darkened by moving the (s,v) values (in hsv colorspace) toward (hsv_min_sat, hsv_min_val) in the shaded regions, or lightened by sliding (s,v) toward (hsv_max_sat hsv_max_val) in regions that are illuminated. The default extremes are chose so that completely shaded points are nearly black (s = 1, v = 0) and completely illuminated points are nearly white (s = 0, v = 1).
Take the input data array, convert to HSV values in the given colormap, then adjust those color values to given the impression of a shaded relief map with a specified light source. RGBA values are returned, which can then be used to plot the shaded image with imshow.
Take the input RGB array (ny*nx*3) adjust their color values to given the impression of a shaded relief map with a specified light source using the elevation (ny*nx). A new RGB array ((ny*nx*3)) is returned.
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.
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.
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.
Set a new gamma value and regenerate color map.
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.
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.
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.
Set vmin, vmax to min, max of A.
autoscale only None-valued vmin or vmax
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.
Bases: object
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.
Set vmin, vmax to min, max of A.
autoscale only None-valued vmin or vmax
Homogenize the input value for easy and efficient normalization.
value can be a scalar or sequence.
Returns result, is_scalar, where result is a masked array matching value. Float dtypes are preserved; integer types with two bytes or smaller are converted to np.float32, and larger types are converted to np.float. Preserving float32 when possible, and using in-place operations, can greatly improve speed for large arrays.
Experimental; we may want to add an option to force the use of float32.
return true if vmin and vmax set
Take a hex string s and return the corresponding rgb 3-tuple Example: #efefef -> (0.93725, 0.93725, 0.93725)
convert hsv values in a numpy array to rgb values both input and output arrays have shape (M,N,3)
Return True if c can be converted to RGB
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.
Alternatively, data can be a function mapping values between 0 - 1 to 0 - 1.
The function returns an array “result” where result[x*(N-1)] gives the closest value for values of x between 0 and 1.
Given an rgb or rgba sequence of 0-1 floats, return the hex string
convert rgb values in a numpy array to hsv values input and output arrays should have shape (M,N,3)