Introduction
Almost all the programmers who work with Python programming language know Matplotlib. It is one of the most used libraries. It is a multi-platform library that can play with many operating systems and was built in 2002 by John Hunter.
“Matplotlib is a multi-platform library”
Nowadays, people start to develop new packages with more simple and more modern styles than in Matplotlib, like Seaborn, Plotly, and even Pandas uses Matplotlib’s API wrappers. But, I think Matplotlib still in many programmer’s hearts.
If you need to learn the introductory in using Matplotlib, you can check this link out-
Colormaps in Matplotlib
In visualizing the 3D plot, we need colormaps to differ and make some intuitions in 3D parameters. Scientifically, the human brain perceives various intuition based on the different colors they see.
Matplotlib provides some nice colormaps you can use, such as Sequential colormaps, Diverging colormaps, Cyclic colormaps, and Qualitative colormaps. For practical purposes, I did not explain in more detail the differences among them. I think it will be simple if I show you the examples of each categorical colormaps in Matplotlib.
Here are some examples (not all) of Sequential colormaps.
Matplotlib will give you viridis as a default colormaps.
Then, next are the examples of Diverging, Cyclic, Qualitative, and Misc colormaps in Matplotlib.
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.
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.
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.

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
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
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.
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
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’.
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.
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
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
After that, you can combine it using the previous methods.
newcolors2 = np.vstack((yellow_cmp(np.linspace(0, 1, 128)), red_cmp(np.linspace(1, 0, 128))))double = ListedColormap(newcolors2, name='double')plt.figure(figsize=(7, 6))plt.pcolormesh(data, cmap=double) plt.colorbar()
You will get a figure like this
You can also adjust the orientation, the extend, and the pad distance of the colormaps using this code.
plt.figure(figsize=(6, 7))plt.pcolormesh(data, cmap = double) plt.colorbar(orientation = 'horizontal', label = 'My Favourite Colormaps', extend = 'both', pad = 0.1)
You will be shown a figure like this
That’s all. Thank you.
About the Author
Rizky Maulana Nurhidayat
My name is Rizky Maulana Nurhidayat, a founder of LeafTech. I am a Bayesian astronomer working with big data in Astronomy to reveal our galaxy’s hidden patterns, the Milky Way.

