Creating Colormaps in Matplotlib
By: Rizky Maulana Nurhidayat
Tags: Data Visualization, Colors, Data Science, Matplotlib, Python

Your own colormaps

Are you not interested in all of the provided colormaps? Or you need other fancy colormaps? If yes, you need to read this article until the end. I will guide you through customizing and creating your own colormaps.

But before customizing it, I will show you an example of colormaps use. I used the ‘ RdYlBu_r ‘ colormaps to visualize my data.

Example of RdYlBu_r colormaps in Matplotlib (Image by Author)

Let’s modify your own colormaps.

Firstly, we need to create mock data that will be visualized, using this code

# import some libraries / modules

import numpy as np

import matplotlib.pyplot as plt # create mock data

data = np.random.random([100, 100]) * 10

The data variable is an array that consists of 100 x 100 random numbers from 0–10. You can check it by writing this code.

Mock data to be visualized (Image by Author)

After that, we will show the mock data with a default colormaps using the simple code below.

plt.figure(figsize=(7, 6))

plt.pcolormesh(data)

plt.colorbar()

The code will show you a figure like this.

Colormesh in visualizing the mock data with default colormaps (Image by Author)

As I mentioned before, if you didn’t define the colormaps you used, you will get the default colormaps, named ‘ viridis ‘.

Next, I will change the colormaps from ‘ viridis ‘ to ‘inferno’ colormaps with this code

plt.pcolormesh(data, cmap='inferno')

You will get a result like this

Colormesh in visualizing the mock data with ‘inferno’ colormaps

Modifying Colormaps

Now, to modify the colormaps, you need to import these following sublibraries in Matplotlib.

from matplotlib import cm

from matplotlib.colors import ListedColormap,LinearSegmentedColormap

To modify the number of color class in your colormaps, you can use this code

new_inferno = cm.get_cmap('inferno', 5) # visualize with the new_inferno colormaps

plt.pcolormesh(data, cmap = new_inferno)

plt.colorbar()

and will get a result like this

Modified inferno with just 5 classes color (Image by Author).

Next is modifying the range color in a colormap. I will give you an example in ‘hsv’ colormaps. You need to understand the range of colors using this figure.

hsv colormaps (Image by Author)

If we want to use only green color (about 0.3) to blue color (0.7), we can use the following code.

# modified hsv in 256 color class

hsv_modified = cm.get_cmap('hsv', 256) # create new hsv colormaps in range of 0.3 (green) to 0.7 (blue)

newcmp = ListedColormap(hsv_modified(np.linspace(0.3, 0.7, 256))) # show figure

plt.figure(figsize=(7, 6))

plt.pcolormesh(data, cmap = newcmp)

plt.colorbar()

It will give you a figure like this

Modify the range color of ‘hsv’ colormaps (Image by Author)

Create your own colormaps

To create your own colormaps, there are at least two methods. First, you can combine two Sequential colormaps in Matplotlib. Second, you can choose and combine your favorite color in RGB to create colormaps.

We will give you a demo in combining two Sequential colormaps to create a new colormap. We want to combine ‘Oranges’ and ‘Blues’.

Combining Blues and Oranges colormaps to create a new one (Image by Author)

You can read this code carefully.

# define top and bottom colormaps

top = cm.get_cmap('Oranges_r', 128) # r means reversed version

bottom = cm.get_cmap('Blues', 128) # combine it all

newcolors = np.vstack((top(np.linspace(0, 1, 128)),

bottom(np.linspace(0, 1, 128)))) # create a new colormaps with a name of OrangeBlue

orange_blue = ListedColormap(newcolors, name='OrangeBlue')

If you visualize the mock data using ‘OrangeBlue’ colormaps, you will get a figure like this.

OrangeBlues colormaps (Image by Author)

Next is creating a colormap from two different color you likes. In this case, I will try to create it from yellow and red color as shown in the following picture

Two colors that will be combined (Image by Author)

First, you need to create yellow colormaps

# create yellow colormaps N = 256 yellow = np.ones((N, 4)) yellow[:, 0] = np.linspace(255/256, 1, N) # R = 255

yellow[:, 1] = np.linspace(232/256, 1, N) # G = 232

yellow[:, 2] = np.linspace(11/256, 1, N) # B = 11 yellow_cmp = ListedColormap(yellow)

and red colormaps

red = np.ones((N, 4)) red[:, 0] = np.linspace(255/256, 1, N)

red[:, 1] = np.linspace(0/256, 1, N)

red[:, 2] = np.linspace(65/256, 1, N) red_cmp = ListedColormap(red)

The visualization of yellow and red colormaps you have created is shown in the following picture