Related Articles

Related Articles

Matplotlib.colors.ListedColormap class in Python
  • Last Updated : 21 Apr, 2020

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.

Matplotlib.colors.ListedColormap

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 colarmap 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 equat 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 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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",
         "red"]
  
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)
figure.colorbar(contour)
  
plt.show()

chevron_right


Output:
matplotlib.colors.ListedColormap

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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],
                         u[:-1]+np.diff(u)/2.,
                         [res.max()+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.)
color_bar.ax.set_yticklabels(color_map1)
color_bar.ax.tick_params(labelsize = 10)
  
plt.show()

chevron_right


Output:
matplotlib.colors.ListedColormap

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :