Matplotlib.colors.Colormap class in Python

Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack.


The matplotlib.colors.Colormap class belongs to the matplotlib.colors module. The matplotlib.colors module is used for converting color or numbers arguments to RGBA or RGB.This module is used for mapping numbers to colors or color specification conversion in a 1-D array of colors also known as colormap.

The matplotlib.colors.Colormap class is a base class for all scalar to RGBA mappings. Generally colormap instance are used to transform data values (floats) from interval 0-1 to their respective RGBA color. Here the matplotlib.colors.Normalize class is used for scaling the data. The subclasses heavily use this for data->normalize->map-to-color processing chain.


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


  • name: It accepts a string that represents the name of the color.
  • N: It is an integer value that represents the number of rgb quantization levels.

Methods of Class:

  1. colorbar_extend = None : If the colormap exists on a scalar mappable and colorbar_extend is set to false, colorbar_extend is picked up by colorbar creation as the default value for the extend keyword in the constructor of matplotlib.colorbar.Colorbar.
  2. is_gray(self): Returns a boolean value to check if the plt is gray.
  3. reversed(self, name=None): It is used to make a reversed instance of Colormap. This function is not implemented for the base class. It has a single parameter ie, name that is optional and and accepts a string name for the reversed colormap. If set to None the becomes the name of the parent colormap + “r”.
  4. set_bad(self, color=’k’, alpha=None): It sets color that is to be used for masked values.
  5. set_over(self, color=’k’,, alpha=None): It is used to set color to be used for high out-of-range values. It requires norm.clip to be False.
  6. set_under(self, color=’k’,, alpha=None):It is used to set color to be used for low out-of-range values. It requires norm.clip to be False.






import numpy as np
import matplotlib.pyplot as plt
start_point = 'lower'
diff = 0.025
a = b = np.arange(-3.0, 3.01, diff)
A, B = np.meshgrid(a, b)
X1 = np.exp(-A**2 - B**2)
X2 = np.exp(-(A - 1)**2 - (B - 1)**2)
X = (X1 - X2) * 2
RR, RC = X.shape
# putting NaNs in one corner:
X[-RR // 6:, -RC // 6:] = np.nan
X =
# masking the other corner:
X[:RR // 6, :RC // 6] =
# masking a circle in the middle:
INNER = np.sqrt(A**2 + B**2) < 0.5
# using automatic selection of
# contour levels;
figure1, axes2 = plt.subplots(constrained_layout = True)
C = axes2.contourf(A, B, X, 10
                   cmap =, 
                   origin = start_point)
C2 = axes2.contour(C, levels = C.levels[::2], 
                   colors ='r', origin = start_point)
axes2.set_title('3 masked regions')
axes2.set_xlabel('length of word anomaly')
axes2.set_ylabel('length of sentence anomaly')
# Make a colorbar for the ContourSet 
# returned by the contourf call.
cbar = figure1.colorbar(C)'coefficient of verbosity')
# Add the contour line levels
# to the colorbar
figure2, axes2 = plt.subplots(constrained_layout = True)
# making a contour plot with the
# levels specified,
levels = [-1.5, -1, -0.5, 0, 0.5, 1]
C3 = axes2.contourf(A, B, X, levels,
                   colors =('r', 'g', 'b'),
                   origin = start_point,
                   extend ='both')
# data below the lowest contour 
# level yellow, data below the
# highest level green:
C4 = axes2.contour(A, B, X, levels,
                  colors =('k', ),
                  linewidths =(3, ),
                  origin = start_point)
axes2.set_title('Listed colors (3 masked regions)')
axes2.clabel(C4, fmt ='% 2.1f'
             colors ='w',
             fontsize = 14)
# Illustrating all 4 possible 
# "extend" settings:
extends = ["neither", "both", "min", "max"]
cmap ="winter")
figure, axes = plt.subplots(2, 2,
                            constrained_layout = True)
for ax, extend in zip(axes.ravel(), extends):
    cs = ax.contourf(A, B, X, levels, 
                     cmap = cmap, 
                     extend = extend, 
                     origin = start_point)
    figure.colorbar(cs, ax = ax, shrink = 0.9)
    ax.set_title("extend = % s" % extend)
    ax.locator_params(nbins = 4)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.