Matplotlib Colormap

Matplotlib Colormap

When creating visualizations using the Matplotlib library, choosing the right colors is crucial for effectively conveying your data. Matplotlib provides a range of predefined colormaps, which are essential in representing data relationships and patterns accurately.

What is a Colormap in Matplotlib?

A colormap is a lookup table that maps a range of data values to a range of colors. Each color in the colormap represents a unique value, allowing us to visually distinguish between different levels or categories of data. A colormap is often represented as a gradient, where the colors smoothly transition from one to another.

Matplotlib offers a wide variety of predefined colormaps, each designed for specific purposes. These colormaps can be used in various plot types, such as scatter plots, line plots, heatmaps, bar plots, etc.

In this article, we will explore some of the commonly used Matplotlib colormaps and how to effectively use them in your visualizations.

Getting Started

Before diving into the code examples, let’s begin by installing Matplotlib if you haven’t already:

!pip install matplotlib

Now that Matplotlib is installed, let’s import it along with NumPy, a popular numerical computing library, to facilitate the creation of sample data:

import matplotlib.pyplot as plt
import numpy as np

1. Default Matplotlib Colormap

Matplotlib provides a default colormap that is used when no specific colormap is defined. The default colormap is called viridis and is a perceptually uniform colormap, meaning that the perceived change in color is equal across the entire range.

To create a heat map using the default colormap, we can use the imshow function:

import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(10, 10)
plt.imshow(data, cmap='viridis')
plt.colorbar()
plt.show()

The imshow function takes the sample data and assigns colors to individual values based on the colormap specified (viridis in this case). The colorbar function adds a colorbar representing the mapping between data values and colors.

Matplotlib Colormap

2. Sequential Matplotlib Colormaps

Sequential colormaps are often used when representing continuous data, where values gradually increase or decrease. Examples include temperature maps, elevation maps, and so on. Let’s explore a few commonly used sequential colormaps:

2.1. inferno

The inferno colormap has a dark color for low values and gradually transitions to bright colors for high values. It is especially useful for emphasizing patterns in data with a wide range of values.

data = np.linspace(0, 1, 100).reshape(10, 10)
plt.imshow(data, cmap='inferno')
plt.colorbar()
plt.show()

Matplotlib Colormap

2.2. plasma

The plasma colormap, similar to inferno, is also useful for showing patterns in data with a wide range of values. It has a dark-to-light gradient where darker colors represent low values and brighter colors represent high values.

data = np.linspace(0, 1, 100).reshape(10, 10)
plt.imshow(data, cmap='plasma')
plt.colorbar()
plt.show()

Matplotlib Colormap

2.3. magma

The magma colormap is highly perceptually uniform and is particularly effective when representing data with varying intensities. It has dark colors for low values and vibrant colors for high values, providing a visually appealing representation.

data = np.linspace(0, 1, 100).reshape(10, 10)
plt.imshow(data, cmap='magma')
plt.colorbar()
plt.show()

Matplotlib Colormap

2.4. cividis

The cividis colormap is a newer addition to Matplotlib and is designed specifically for colorblindness accessibility. It is a perceptually uniform colormap with a smooth transition from dark to light. It is recommended for representing sequential data to ensure clarity for all users.

data = np.linspace(0, 1, 100).reshape(10, 10)
plt.imshow(data, cmap='cividis')
plt.colorbar()
plt.show()

Matplotlib Colormap

2.5. binary

The binary colormap is a simple two-color colormap consisting of black and white. It is commonly used when representing binary data, such as binary images, where black represents 0 and white represents 1.

data = np.random.randint(0, 2, size=(10, 10))
plt.imshow(data, cmap='binary')
plt.show()

Matplotlib Colormap

3. Diverging Matplotlib Colormaps

Diverging colormaps are used when representing data that has two distinct extremes with a meaningful midpoint. These colormaps typically have two different colors at each end, with a clear midpoint color. Diverging colormaps are commonly used for visualizations where it is important to highlight deviations from a midpoint or compare two opposing data sets.

3.1. coolwarm

The coolwarm colormap has cool colors (blues) for low values, warm colors (reds) for high values, and white for the midpoint. This colormap is commonly used to represent temperature differences, such as temperature anomalies or climate data.

data = np.random.randn(10, 10)
plt.imshow(data, cmap='coolwarm')
plt.colorbar()
plt.show()

Matplotlib Colormap

3.2. PiYG

The PiYG colormap consists of a combination of pink and green colors. The midpoint is white, with shades of pink for low values and shades of green for high values. This colormap is often used to represent positive and negative deviations from a mean value.

data = np.random.randn(10, 10)
plt.imshow(data, cmap='PiYG')
plt.colorbar()
plt.show()

Matplotlib Colormap

3.3. RdBu

The RdBu colormap represents data with two contrasting colors: dark blue for low values, white for the midpoint, and dark red for high values. It is commonly used to highlight positive and negative deviations or to represent signed values.

data = np.random.randn(10, 10)
plt.imshow(data, cmap='RdBu')
plt.colorbar()
plt.show()

Matplotlib Colormap

3.4. seismic

The seismic colormap is similar to RdBu but with more pronounced contrast. It is particularly useful for emphasizing positive and negative deviations or representing seismic data, hence the name.

data = np.random.randn(10, 10)
plt.imshow(data, cmap='seismic')
plt.colorbar()
plt.show()

Matplotlib Colormap

3.5. BrBG

The BrBG colormap combines shades of brown and green. It is useful for representing data that has distinct positive and negative values, with the midpoint color denoted by white.

data = np.random.randn(10, 10)
plt.imshow(data, cmap='BrBG')
plt.colorbar()
plt.show()

Matplotlib Colormap

These are just a few of the commonly used colormaps available in Matplotlib. Experiment with different colormaps to find the ones that best suit your data and visualization requirements.

Matplotlib Colormap Conclusion

In this article, we explored how to effectively use colormaps in Matplotlib to enhance data visualization. We covered sequential colormaps, which are ideal for representing continuous data, and diverging colormaps, which highlight deviations from a midpoint. Understanding the different colormaps available and their applications can greatly improve the effectiveness and clarity of your visualizations. Remember to choose colormaps that are appropriate for your data type and the message you want to convey.

You now have the tools to create visually appealing and informative visualizations using Matplotlib colormaps.

Like(3)