The colormaps are collections of colors used to map data values to colors. They are very useful for visualizing data on a gradient, such as heatmaps or surface plots. The matplotlib offers sequential colormaps, diverging colormaps, qualitative colormaps, and cyclic colormaps. The list of all colormaps can be viewed with the following code.
import matplotlib.pyplot as plt plt.colormaps()
Although the colormaps function will list all the colormaps defined these coloramps can be split into several categories based on their function:
Sequential colormaps - are used for data that rangesfrom low to high, with a gradient of colors. The perceptually uniform sequential colormaps are viridis, plasma, inferno, magma, and cividis. The sequential colormaps are 'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'.
Divergining colormaps - are used when the data has a central value and you want the deviation on either side.
Cyclic colormaps - Cyclic colormaps in Matplotlib are designed for data that wraps around cyclically, such as angles (0° to 360°) or phase data. These colormaps start and end with the same color to emphasize continuity.
Qualitative colorampas -
Qualitative colormaps are used for categorical data and consist of distinct, non-sequential colors. Each color represents a different category, with no inherent order or gradient between them.
The colormaps can be applied in matplotlib functions such as imshow, contourf, and scatter. The matplotlib.pyplot.imshow() function in Matplotlib is used to dispaly image data (e.g., a 2D array or image) as a heatmap or image. The full form with default argument valuesa is written below.
Before describing each of the function arguments it should be noted that X is the only parameter required and this is the image data. Other argumetns are optinal so if you do not define them they will be set to default value shown in previous code block.
As seen from the imshow function the foolowing key arguments are:
X - is the main input, a 2D array or image-like data to be displayed. The array's values are mapped to colors based on the color map.
cmap (default value: None) - this argument specifies the colormap that will be used for mapping data values to colors. For example cmap = 'viridis' or cmap = 'gray'
norm (default: None) - A normalization object to scale data values to the [0,1] range before applying the colormap. Often used with matplotlib.colors.Normalize
aspect (default: None) - Controls the aspect ratio of the image. Options are auto and equal. The Auto adjust the aspect ratio to fit the axes while the "equal" ensures equal scaling for x and y axes.
interpolation (default: None) - Determines how pixel values are interpolated. Common values: "nearest", "bilinear", "bicubic"
alpha (default: None) - sets the opacity of hte image (0 for fully transparent, 1 for fully opaque).
vmin and vmax (default: None) - set the minimum (vmin) and maximum (vmax) data values to normalize the colormap. Use for controlling color scaling.
origin (default: None) - defines the placement of [0,0] in the data array. The 'upper' value places the origin in the top-left(default for images). The 'lower' places the origin in the bottom-left.
extent (default: None) - specifies the bounding box of the image in data coordinates: [left, right, bottom, top]. Changes how the image is scaled and placed on the axes.
interpolation_stage (default: None) - Controls when interpolation is applied. If the interpolation is set to "data" the interpolation ahppens before colormap application. If the interpolation is set to "rgba" then the interpolation happens after applying the colormap.
Filternorm (default: True) - whether the resampling filter should normalize the weights
filterrad (default: 4.0) - sets the filter radius for resmapling.
resample (default: None) - Indicates whether to resample the image when displayed. Useful for scaling.
url (default: None) - a URL associated with the image for interactivity.
data (defualt: None) - a keyword to pass additional data to the function using the data parameter.
In the following example we will create a 10x10 (10 rows by 10 columns) dataset of random values that will be shown in form of heatmap using the imshow function. The first step is to define the numpy and matplotlib libraries, since only these libraires will be required to successfully executed the code.
import numpy as np import matplitlib.pyplot as plt
The dataset 10x10 with random numbers in 0 to 1 range is generated using np.random.rand() function.
data = np.random.rand(10,10)
Finally the code for showing the plot is required which is in this case the plt.imshow() for defining the plot, the plt.colorbar() to add the colobar to show the scale. Finally, the plt.show() function is used to display photograph or video.
plt.imshow(data, cmap='viridis', interpolation='nearest', aspect='auto') plt.colorbar() # Add colorbar to show the scale plt.show()
The entire code used in this example is shown below.
import matplotlib.pyplot as plt import numpy as np # Create a random 2D array data = np.random.rand(10, 10) # Display the array as an image plt.imshow(data, cmap='viridis', interpolation='nearest', aspect='auto') plt.colorbar() # Add colorbar to show the scale plt.show()
The result of this example is shown in Figure 1. This example visualizes 2D array as an image useing the "viridis" colormap and nearest neighbor interpolation.
Figure 1 - The example of using the viridis colormap in imshow() function