matplotlib.colors.Colormap#

classmatplotlib.colors.Colormap(name,N=256,*,bad=None,under=None,over=None)[source]#

Bases:object

Baseclass for all scalar to RGBA mappings.

Typically, Colormap instances are used to convert data values (floats)from the interval[0,1] to the RGBA color that the respectiveColormap represents. For scaling of data into the[0,1] interval seematplotlib.colors.Normalize. Subclasses ofmatplotlib.cm.ScalarMappablemake heavy use of thisdata->normalize->map-to-color processingchain.

Parameters:
namestr

The name of the colormap.

Nint

The number of RGB quantization levels.

badcolor, default: transparent

The color for invalid values (NaN or masked).

Added in version 3.11.

undercolor, default: color of the lowest value

The color for low out-of-range values.

Added in version 3.11.

overcolor, default: color of the highest value

The color for high out-of-range values.

Added in version 3.11.

__call__(X,alpha=None,bytes=False)[source]#
Parameters:
Xfloat or int or array-like

The data value(s) to convert to RGBA.For floats,X should be in the interval[0.0,1.0] toreturn the RGBA valuesX*100 percent along the Colormap line.For integers,X should be in the interval[0,Colormap.N) toreturn RGBA valuesindexed from the Colormap with indexX.

alphafloat or array-like or None

Alpha must be a scalar between 0 and 1, a sequence of suchfloats with shape matching X, or None.

bytesbool, default: False

If False (default), the returned RGBA values will be floats in theinterval[0,1] otherwise they will benumpy.uint8s in theinterval[0,255].

Returns:
Tuple of RGBA values if X is scalar, otherwise an array of
RGBA values with a shape ofX.shape+(4,).
colorbar_extend#

When this colormap exists on a scalar mappable and colorbar_extendis not False, colorbar creation will pick upcolorbar_extend asthe default value for theextend keyword in thematplotlib.colorbar.Colorbar constructor.

copy()[source]#

Return a copy of the colormap.

get_bad()[source]#

Get the color for masked values.

get_over()[source]#

Get the color for high out-of-range values.

get_under()[source]#

Get the color for low out-of-range values.

is_gray()[source]#

Return whether the colormap is grayscale.

resampled(lutsize)[source]#

Return a new colormap withlutsize entries.

reversed(name=None)[source]#

Return a reversed instance of the Colormap.

Note

This function is not implemented for the base class.

Parameters:
namestr, optional

The name for the reversed colormap. If None, thename is set toself.name+"_r".

set_bad(color='k',alpha=None)[source]#

[Deprecated] Set the color for masked values.

Notes

Deprecated since version 3.11:Use cmap.with_extremes(bad=...) or Colormap(bad=...) instead.

set_extremes(*,bad=None,under=None,over=None)[source]#

[Deprecated] Set the colors for masked (bad) values and, whennorm.clip=False, low (under) and high (over) out-of-range values.

Notes

Deprecated since version 3.11:Use cmap.with_extremes(bad=..., under=..., over=...) or Colormap(bad=..., under=..., over=...) instead.

set_over(color='k',alpha=None)[source]#

[Deprecated] Set the color for high out-of-range values.

Notes

Deprecated since version 3.11:Use cmap.with_extremes(over=...) or Colormap(over=...) instead.

set_under(color='k',alpha=None)[source]#

[Deprecated] Set the color for low out-of-range values.

Notes

Deprecated since version 3.11:Use cmap.with_extremes(under=...) or Colormap(under=...) instead.

with_alpha(alpha)[source]#

Return a copy of the colormap with a new uniform transparency.

Parameters:
alphafloat

The alpha blending value, between 0 (transparent) and 1 (opaque).

with_extremes(*,bad=None,under=None,over=None)[source]#

Return a copy of the colormap, for which the colors for masked (bad)values and, whennorm.clip=False, low (under) and high (over)out-of-range values, have been set accordingly.

Examples usingmatplotlib.colors.Colormap#

Create a colormap from a list of colors

Create a colormap from a list of colors

Selecting individual colors from a colormap

Selecting individual colors from a colormap

Lasso Demo

Lasso Demo

Contourf demo

Contourf demo

Left ventricle bullseye

Left ventricle bullseye

Customized Colorbars Tutorial

Customized Colorbars Tutorial

Creating Colormaps in Matplotlib

Creating Colormaps in Matplotlib