When Graphic Designers Meet Matplotlib – Creating Colormaps using Matplotlib in 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.

create your own colormaps matplotlib

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.

create your own colormaps

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.

create your own colormaps matplotlib

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 the result like this.viridis

create and modify your own colormaps matplotlib


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

modify and create your own colormaps in matplotlib

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.

Image for post

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 and create colormaps in matplotlib

Creating 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 colormaps in matplotlib

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.

Create and modify your own colormaps in matplotlib

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

combining colormaps in matplotlib

First, you need to create yellow colormaps

# create yellow colormapsN = 256yellow = 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 = 11yellow_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

Author: admin

Leave a Reply

Your email address will not be published.