Matplotlib.colors.ListedColormap class in Python - GeeksforGeeks

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.
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:
import matplotlib.pyplot as pltimport numpy as npimport 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()
Output:
Example 2:
import matplotlib.pyplot as pltimport numpy as npimport matplotlib.colors as colorsfrom 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()
Output:
Python-matplotlib
Python
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Convert integer to string in Python
Convert string to integer in Python
Python infinity
How to set input type date in dd-mm-yyyy format using HTML ?
Matplotlib.pyplot.title() in Python