Color maps in Matplotlib | When graphic designers meet Matplotlib

Share on facebook
Share on twitter
Share on linkedin
Share on telegram
Share on whatsapp

Contents

Your own color maps

Not interested in all the color maps provided? Or do you need other fancy color maps? If so, you must read this post to the end. I'll walk you through customizing and creating your own color maps.

But before customizing it, I will show you an example of using color maps. I used the ‘RdYlBu_r‘Color maps to visualize my data.

1repkpehdtxe5fnu_hqcdwq-9117981

Let's modify your own color maps.

First, we need to create simulated data that will be displayed, 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 consisting of 100 x 100 random numbers from 0 al 10. You can check it by writing this code.

1raeoqngyzq9yat_wcpwtsw-5346265

Thereafter, we will display the simulated data with 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.

1tfqp4c5biki3t9pkbgbyzg-4916891

As i mentioned before, if you did not define the color maps you used, you will get the default colormaps, called ‘viridis‘.

Then, I will change the color maps of ‘viridis‘a’ inferno 'color maps with this code-

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

You will get the result like this. Viridis

1epy4iobkukeblowmmffuxw-3368622


Modify color maps

Now, to modify the color maps, you need to import these following sub-libraries into Matplotlib.

from matplotlib import cm
from matplotlib.colors import ListedColormap,LinearSegmentedColormap

To modify the color class number in your color maps, 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 you will get a result like this

1n-yfpe64xds4tcvf3v06q-4397422

The next thing is to modify the color of the range in a colormap. I will give you an example in color maps ‘hsv’. You need to understand the color gamut with this figure.

1knljmcykkacavlqwj-nqla-4189041

If we want to use only the green color (about 0.3) to the 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(e.g. 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

19ntdb0aolk9bhlbwmqs2cw-9548003

Creating your own color maps

To create your own color maps, there are at least two methods. First, you can combine two sequential colormaps in Matplotlib. Secondly, you can select and match your favorite color in RGB to create color maps.

We will give you a demonstration of combining two sequential color maps to create a new color map.. We want to combine ‘Oranges’ and 'Blue'.

1rlilduduamx11de4uxoumg-4097986

You can read this code carefully.

# establece 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(e.g. linspace(0, 1, 128)),
                       bottom(e.g. linspace(0, 1, 128))))# create a new colormaps with a name of OrangeBlue
orange_blue = ListedColormap(newcolors, name="OrangeBlue")

If you view the simulated data using ‘OrangeBlue’ color maps, you will get a figure like this.

1wncg6ygkxwhaztqtu14hww-1044333

The next thing is to create a colormap from two different colors that you like. For this case, I will try to create it from yellow and red color as shown in the next image.

1virwxzf6rw2olkbdd0wltg-1192361

First, you need to create yellow maps

# create yellow colormapsN = 256yellow = np.ones((N, 4))yellow[:, 0] = e.g. linspace(255/256, 1, N) # R = 255
yellow[:, 1] = e.g. linspace(232/256, 1, N) # G = 232
yellow[:, 2] = e.g. linspace(11/256, 1, N)  # B = 11yellow_cmp = ListedColormap(yellow)

and red maps

red = np.ones((N, 4))red[:, 0] = e.g. linspace(255/256, 1, N)
red[:, 1] = e.g. linspace(0/256, 1, N)
red[:, 2] = e.g. linspace(65/256, 1, N)red_cmp = ListedColormap(red)

The yellow and red map display you created is shown in the next image

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.