Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

Matplotlib.colors.ListedColormap class in Python

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.ListedColormap 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.ListedColormap class is used to create colormap objects from a list of colors. This can be useful for directly indexing into colormap and it can also be used to create special colormaps for normal mapping.

Syntax: class matplotlib.colors.ListedColormap(colors, name=’from_list’, N=None) Parameters:

  • colors: It is an array or list of Matplotlib color specifications or equal to N x 3 or N x 4 floating point array(N rgb or rgba values)
  • name: It is an optional parameter that accepts a string to identify the colormap.
  • N: It is an optional parameter that accepts an integer value representing the number of entries in the map. Its default is None, where there is single colormap entry for every element in the list of colors. If N is less than len(colors) the list truncates at N whereas if N is greater than the list is extended with repetition.

Methods of the class: 1) reversed(): This is used to create a reversed instance of the Colormap.

Syntax: reversed(self, name=None) Parameters:

  • name: It is an optional parameter that represents the name for the reversed colormap. If it’s set to None the name will be the name of the parent colormap + “_r”.

Returns: It returns a reversed instance of the colormap

Example 1: 


import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors
a = np.linspace(-3, 3)
A, B = np.meshgrid(a, a)
X = np.exp(-(A**2 + B**2))
figure, (axes1, axes2) = plt.subplots(ncols = 2)
colors =["green", "orange",
         "gold", "blue", "k",
        "#550011", "purple",
axes1.set_title(" color list")
contour = axes1.contourf(A, B, X,
                         colors = colors)
axes2.set_title("with colormap")
cmap = matplotlib.colors.ListedColormap(colors)
contour = axes2.contourf(A, B, X, cmap = cmap)

Output: matplotlib.colors.ListedColormap Example 2: 


import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as colors
from mpl_toolkits.axes_grid1 import make_axes_locatable
res = np.array([[0, 2], [3, 4]], dtype = int)
u = np.unique(res)
bounds = np.concatenate(([res.min()-1],
norm = colors.BoundaryNorm(bounds, len(bounds)-1)
color_map1 = ['#7fc97f', '#ffff99',
              '#386cb0', '#f0027f']
color_map = colors.ListedColormap(color_map1)
fig, axes = plt.subplots()
img = axes.imshow(res, cmap = color_map,
                  norm = norm)
divider = make_axes_locatable(axes)
cax = divider.append_axes("right", size ="5 %")
color_bar = plt.colorbar(img, cmap = color_map,
                         norm = norm, cax = cax)
color_bar.set_ticks(bounds[:-1]+np.diff(bounds)/2.) = 10)

Output: matplotlib.colors.ListedColormap

My Personal Notes arrow_drop_up
Last Updated : 22 Dec, 2022
Like Article
Save Article
Similar Reads
Related Tutorials