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.
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.
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.
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-
You will get the result like this. Viridis
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
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.
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
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'.
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.
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.
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