Matplotlib is a popular Python library used for data visualization and plotting. One of the most common tasks when using matplotlib is to set the colormap (also called the color map) for a plot. The colormap controls the colors used to map data values to colors in the plot. Matplotlib provides a wide range of builtin colormaps to choose from. In this article, we will explore the various colormap options available in matplotlib and how to use them effectively for data visualization.
Introduction to Colormaps
A colormap is essentially a mapping from data values to colors. It is used to assign colors to plot elements like lines, surfaces, scatter points etc based on the data values associated with them. For example, in a surface plot of a 2D function z=f(x,y), the colormap assigns colors to pixels in the plot based on the z-values computed from the function f(x,y) at those pixel locations.
Some common uses of colormaps are:
- To visualize height in a surface plot
- To distinguish different categories in a scatter plot
- To encode values in a filled contour plot
- To show intensity or concentration with pseudocolor plots
Matplotlib has a module dedicated to colormaps called matplotlib.cm. This module contains a collection of predefined colormaps that can be used in matplotlib visualizations. The cmap parameter in matplotlib plotting functions is used to specify the colormap. There are also helper functions like colormap.ScalarMappable that can map data values to colormap colors.
Types of Colormaps
Matplotlib colormaps can be broadly divided into three categories:
These map data values to colors along a sequential spectrum. Low data values are mapped to colors at one end of the spectrum while high data values are mapped to colors at the other end. Sequential colormaps are good for visualizing data that has a natural ordering or organizing data from low to high values. Some common sequential colormaps are:
- viridis – ranges from blue to yellow
- plasma – ranges from purple to yellow
- inferno – ranges from black to yellow
- magma – ranges from black to white
These map low and high data values to distinct colors with a transition color in the middle. The transition color is used to mark the critical midpoint value in the data. Diverging colormaps are useful when there is a need to highlight a specific mid value in the data being visualized. Some common diverging colormaps are:
- coolwarm – ranges from blue to red with white midpoint
- PiYG – ranges from pink to green with white midpoint
- PuOr – ranges from purple to orange with white midpoint
- RdBu – ranges from red to blue with white midpoint
These map discrete data values to a set of distinct colors. Categorical colormaps do not imply any ordering of colors but simply use different colors to distinguish between categories. These are useful for visualizing categorical data. Some common categorical colormaps are:
- Set1, Set2, Set3 – color palettes with 9 distinct colors
- tab10, tab20, tab20b, tab20c – color palettes with 10-20 distinct colors
- Paired – color palette with 12 distinct colors
- Pastel1, Pastel2 – soft color palettes
Commonly Used Colormaps
Here are some of the most commonly used colormaps in matplotlib:
This is the default sequential colormap in matplotlib. It is perceptually uniform and colorblind friendly. The colors range from blue to yellow.
This is another perceptual uniform colormap that goes from black to yellow. Useful for visualizing hot or bright data.
Perceptual uniform colormap ranging from black to white, useful for high contrast visualizations.
Diverging colormap from blue to red with white mid point, good for deviating negative/positive values.
Diverging colormap from red to blue with white mid point. Helps highlight the mid value threshold.
Qualitative colormap with 20 distinct colors, useful for visualizing up to 20 discrete categories.
Compact qualitative colormap with 10 colors for fewer categories.
Perceptually Uniform Colormaps
Recent matplotlib versions (>=2.0) have added new colormaps like viridis, magma, inferno and plasma that are perceptually uniform. This means that equal steps in data values are perceived as equal steps visually in the colormap. Traditionally colormaps like jet tend to have uneven perceptual contrast which can distort the visualization. Perceptual uniform colormaps fix this issue and greatly improve ability to understand variations in data. They should be preferred for sequential data.
Colorblind Friendly Colormaps
Some colormaps like viridis are designed to be color blind friendly by avoiding colors that are difficult to distinguish for people with color blindness. When creating visualizations for public use, it is essential to ensure that the colormaps chosen are colorblind friendly. Some options are viridis, plasma and magma for sequential data and coolwarm for diverging data. Avoid rainbow color maps.
Matplotlib also makes it possible to create custom colormaps by specifying the list of colors comprising the colormap. For example:
from matplotlib.colors import LinearSegmentedColormap
cm = LinearSegmentedColormap.from_list('custom', ['red', 'green', 'blue'])
This creates a custom 3 color sequential colormap. We can specify any colors and any number of colors in the list as needed. Custom colormaps give full flexibility in trading off number of colors versus perceptual uniformity.
Colormap Normalization and Scaling
Matplotlib colormaps are generally normalized to map the data values in the range [0, 1] to colors. However, we often need to map custom data ranges to the colormap. This can be done using the vmin and vmax arguments to the plotting functions in matplotlib. For example:
plot(data, vmin=-100, vmax=500, cmap='coolwarm')
This will map data values ranging from -100 to 500 to the coolwarm colormap. Values below -100 will get the lowest colormap color and values above 500 will get the highest colormap color.
The matplotlib.colors.Normalize class can also be used to customize data normalization for the colormaps. This gives more control over scaling including mapping only part of the colormap to data values or applying non-linear scaling.
Diverging Colormaps with a Center Value
For diverging colormaps like coolwarm or RdBu we often want to explicitly set the data value that maps to the center/transition color in the colormap. This is done using the center argument to Normalize. For example:
from matplotlib.colors import Normalize
norm = Normalize(vmin=-100, vmax=150, center=0)
plot(data, cmap='coolwarm', norm=norm)
Here 0 will be mapped to the white midpoint of the coolwarm colormap. Data values between -100 and 0 will get the bluish colors and values between 0 and 150 will get the reddish colors.
Colormap Reference for Categories
Qualitative colormaps that are useful for categorical data are:
|Number of Colors
These provide a wide range of distinct colors for visualizing up to 10-20 categories in plots.
Colormap Reference for Sequential Data
Recommended colormaps for sequential data are:
|Perceptual uniform, from blue to yellow
|Perceptual uniform, from purple to yellow
|Perceptual uniform, from black to yellow
|Perceptual uniform, from black to white
|Perceptual uniform, from blue to yellow
|White to black greyscale
|White to purple
|White to blue
|White to green
|White to orange
|White to red
Colormap Reference for Diverging Data
Recommended colormaps for diverging data around a center point are:
|Blue to red with white midpoint
|Pink to green with white midpoint
|Purple to green with white midpoint
|Purple to orange with white midpoint
|Red to blue with white midpoint
|Red to yellow to blue with white midpoint
|Red to yellow to green with white midpoint
|Red to blue with unsaturated colors
Setting the Default Colormap
The default colormap in matplotlib can be globally set using:
import matplotlib.pyplot as plt
plt.rcParams['image.cmap'] = 'viridis'
This will make viridis the default for all plots created in the session. Similar rcParams exist for specific plot kinds like image, pcolor, scatter etc.
The default can be set back to the original using:
plt.rcParams['image.cmap'] = None
To summarize, effective use of colormaps involves:
- Using perceptually uniform maps like viridis for sequential data
- Using diverging colormaps like coolwarm to highlight a midpoint
- Using categorical maps like tab10 for discrete categories
- Setting perceptually uniform maps like viridis as default
- Avoiding rainbow color maps for continuous data
Matplotlib provides a wide selection of colormaps that cover most data visualization needs. The cmap argument and matplotlib.cm module give access to both builtin and custom colormaps for versatile control over color mapping in plots.
In this article, we looked at the various colormap options available in matplotlib. The key points are:
- Colormaps control the mapping of data values to colors when plotting
- Main colormap types are sequential, diverging and categorical
- Perceptually uniform colormaps like viridis improve visualization
- Diverging colormaps highlight a critical midpoint value
- Categorical colormaps distinguish distinct data classes
- Colormaps can be customized using lists of colors
- Normalization and scaling maps data ranges to colormaps
- Choosing the right colormap is important for effective visualization
Matplotlib’s versatile colormap options along with the ability to create custom maps enables detailed control over the colors used for data visualization in Python plots.