matplotlib.cm
#
Builtin colormaps, colormap handling utilities, and theScalarMappable
mixin.
See also
Colormap reference for a list of builtin colormaps.
Creating Colormaps in Matplotlib for examples of how to makecolormaps.
Choosing Colormaps in Matplotlib an in-depth discussion of choosingcolormaps.
Colormap normalization for more details about data normalization.
- classmatplotlib.cm.ColormapRegistry(cmaps)[source]#
Bases:
Mapping
Container for colormaps that are known to Matplotlib by name.
The universal registry instance is
matplotlib.colormaps
. There should beno need for users to instantiateColormapRegistry
themselves.Read access uses a dict-like interface mapping names to
Colormap
s:importmatplotlibasmplcmap=mpl.colormaps['viridis']
Returned
Colormap
s are copies, so that their modification does notchange the global definition of the colormap.Additional colormaps can be added via
ColormapRegistry.register
:mpl.colormaps.register(my_colormap)
To get a list of all registered colormaps, you can do:
frommatplotlibimportcolormapslist(colormaps)
- get_cmap(cmap)[source]#
Return a color map specified throughcmap.
- Parameters:
- cmapstr or
Colormap
or None if a
Colormap
, return itif a string, look it up in
mpl.colormaps
if None, return the Colormap defined in
rcParams["image.cmap"]
(default:'viridis'
)
- cmapstr or
- Returns:
- Colormap
- register(cmap,*,name=None,force=False)[source]#
Register a new colormap.
The colormap name can then be used as a string argument to any
cmap
parameter in Matplotlib. It is also available inpyplot.get_cmap
.The colormap registry stores a copy of the given colormap, so thatfuture changes to the original colormap instance do not affect theregistered colormap. Think of this as the registry taking a snapshotof the colormap at registration.
- Parameters:
- cmapmatplotlib.colors.Colormap
The colormap to register.
- namestr, optional
The name for the colormap. If not given,
cmap.name
is used.- forcebool, default: False
If False, a ValueError is raised if trying to overwrite an alreadyregistered name. True supports overwriting registered colormapsother than the builtin colormaps.
- unregister(name)[source]#
Remove a colormap from the registry.
You cannot remove built-in colormaps.
If the named colormap is not registered, returns with no error, raisesif you try to de-register a default colormap.
Warning
Colormap names are currently a shared namespace that may be usedby multiple packages. Use
unregister
only if you know youhave registered that name before. In particular, do notunregister just in case to clean the name before registering anew colormap.- Parameters:
- namestr
The name of the colormap to be removed.
- Raises:
- ValueError
If you try to remove a default built-in colormap.
- classmatplotlib.cm.ScalarMappable(colorizer,**kwargs)[source]#
- autoscale_None(self)[source]#
Autoscale the scalar limits on the norm instance using thecurrent array, changing only limits that are None
- changed(self)[source]#
Call this whenever the mappable is changed to notify all thecallbackSM listeners to the 'changed' signal.
- colorbar#
The last colorbar associated with this ScalarMappable. May be None.
- get_array(self)[source]#
Return the array of values, that are mapped to colors.
The base class
ScalarMappable
does not make any assumptions onthe dimensionality and shape of the array.
- propertynorm#
- set_array(self,A)[source]#
Set the value array from array-likeA.
- Parameters:
- Aarray-like or None
The values that are mapped to colors.
The base class
ScalarMappable
does not make any assumptions onthe dimensionality and shape of the value arrayA.
- set_clim(self,vmin=None,vmax=None)[source]#
Set the norm limits for image scaling.
- Parameters:
- vmin, vmaxfloat
The limits.
For scalar data, the limits may also be passed as atuple (vmin,vmax) as a single positional argument.
- set_cmap(self,cmap)[source]#
Set the colormap for luminance data.
- Parameters:
- cmap
Colormap
or str or None
- cmap
- set_norm(self,norm)[source]#
Set the normalization instance.
- Parameters:
- norm
Normalize
or str or None
- norm
Notes
If there are any colorbars using the mappable for this norm, settingthe norm of the mappable will reset the norm, locator, and formatterson the colorbar to default.
- to_rgba(self,x,alpha=None,bytes=False,norm=True)[source]#
Return a normalized RGBA array corresponding tox.
In the normal case,x is a 1D or 2D sequence of scalars, andthe corresponding
ndarray
of RGBA values will be returned,based on the norm and colormap set for this Colorizer.There is one special case, for handling images that are alreadyRGB or RGBA, such as might have been read from an image file.Ifx is an
ndarray
with 3 dimensions,and the last dimension is either 3 or 4, then it will betreated as an RGB or RGBA array, and no mapping will be done.The array can beuint8
, or it can be floats withvalues in the 0-1 range; otherwise a ValueError will be raised.Any NaNs or masked elements will be set to 0 alpha.If the last dimension is 3, thealpha kwarg (defaulting to 1)will be used to fill in the transparency. If the last dimensionis 4, thealpha kwarg is ignored; it does notreplace the preexisting alpha. A ValueError will be raisedif the third dimension is other than 3 or 4.In either case, ifbytes isFalse (default), the RGBAarray will be floats in the 0-1 range; if it isTrue,the returned RGBA array will be
uint8
in the 0 to 255 range.If norm is False, no normalization of the input data isperformed, and it is assumed to be in the range (0-1).