As python programmers, we all know about the matplotlib library. Even a beginner also knows about this library. This is one of the useful libraries in python to plot the graphs and for some visual representation. We can also create matplotlib custom colormap. In this article, we will learn about colormaps using the matplotlib library in a detailed manner. Let us start.
Colormaps are useful for any sort of plot or any sort of visualization. To customize the colormap, we will use a hex bin plot or scatter plot to show the different distributions as different matrices. Custom colormaps using matplotlib are very easy to code. And also this is very easy to learn.
How to access the values of the colormaps?
from matplotlib import cm
from matplotlib.colors import ListedColormap
import numpy as np
import matplotlib.pyplot as plt
winter = cm.get_cmap('winter',5)
print(winter)
print(winter(0.56))
print('winter(range(8))', winter(range(8)))
print('winter(np.linspace(0,1,10))', winter(np.linspace(0, 1,10)))
print('winter(np.linspace(0,1,8))', winter(np.linspace(0, 1,8)))
Import matplotlib library. From matplotlib, import ListedColormap. Import numpy library. Getting a named Colormap. The second argument is for the size of the list of colors. Printing the values of (0,1,10) and (0,1,8).
Output
<matplotlib.colors.LinearSegmentedColormap object at 0x000002623B86D070> (0.0, 0.5, 0.75, 1.0) winter(range(8)) [[0. 0. 1. 1. ] [0. 0.25 0.875 1. ] [0. 0.5 0.75 1. ] [0. 0.75 0.625 1. ] [0. 1. 0.5 1. ] [0. 1. 0.5 1. ] [0. 1. 0.5 1. ] [0. 1. 0.5 1. ]] winter(np.linspace(0,1,10)) [[0. 0. 1. 1. ] [0. 0. 1. 1. ] [0. 0.25 0.875 1. ] [0. 0.25 0.875 1. ] [0. 0.5 0.75 1. ] [0. 0.5 0.75 1. ] [0. 0.75 0.625 1. ] [0. 0.75 0.625 1. ] [0. 1. 0.5 1. ] [0. 1. 0.5 1. ]] winter(np.linspace(0,1,8)) [[0. 0. 1. 1. ] [0. 0. 1. 1. ] [0. 0.25 0.875 1. ] [0. 0.5 0.75 1. ] [0. 0.5 0.75 1. ] [0. 0.75 0.625 1. ] [0. 1. 0.5 1. ] [0. 1. 0.5 1. ]]
Example 1: How to create listed Colormaps?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap
a = cm.get_cmap('winter',8)
a = cm.get_cmap('winter',64)
colors = a(np.linspace(0,1,64))
blue = np.array([250/265, 30/265, 144/265, 1])
colors[:25, :] = blue
newcmp = ListedColormap(colors)
def plot(colormap):
np.random.seed(194201)
x= np.random.randn(50,50)
fig, axis = plt.subplots(1, 2, figsize=(8,4), constrained_layout=True)
for [ax,cmap] in zip(axis,colormap):
a = ax.pcolormesh(x,cmap=cmap, rasterized=True, vmin=-5, vmax=5)
fig.colorbar(a, ax=ax)
plt.show()
plot([a, newcmp])
Import a numpy, matplotlib library. From matplotlib importing cm and listedcolormap. Getting a named Colormap. The second argument is for the size of the list of colors. Creating a function named Colormap. Giving the size of the colormaps. Next, giving the size of the figure as 8,4. Create for loop. Plot the custom color map using matplotlib.
Output
Reduce the Dynamic Range of Colormaps
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap
a = cm.get_cmap('winter',8)
a = cm.get_cmap('winter',64)
colors = a(np.linspace(0,1,64))
red = np.array([250/265, 30/265, 144/265, 1])
colors[:25, :] = red
newcmp = ListedColormap(colors)
def plot(colormap):
np.random.seed(194201)
x= np.random.randn(50,50)
fig, axis = plt.subplots(1, 2, figsize=(8,4), constrained_layout=True)
for [ax,cmap] in zip(axis,colormap):
a = ax.pcolormesh(x,cmap=cmap, rasterized=True, vmin=-5, vmax=5)
fig.colorbar(a, ax=ax)
plt.show()
winterbig = cm.get_cmap('winter', 512)
cmp = ListedColormap(winterbig(np.linspace(0.20, 0.70, 265)))
plot([a, cmp])
Import a numpy, matplotlib library. From matplotlib importing cm and listedcolormap. Getting a named Colormap. The second argument is for the size of the list of colors. Creating a function named Colormap. Giving the size of the colormaps. Next, giving the size of the figure as 8,4. Create for loop. Plot the custom color map using matplotlib. Reduce the dynamic range of colormaps. Plotting the colormaps.
Concatenate two colormaps
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap
a = cm.get_cmap('winter',8)
a = cm.get_cmap('winter',64)
colors = a(np.linspace(0,1,64))
red = np.array([250/265, 30/265, 144/265, 1])
colors[:25, :] = red
newcmp = ListedColormap(colors)
def plot(colormap):
np.random.seed(194201)
x= np.random.randn(50,50)
fig, axis = plt.subplots(1, 2, figsize=(8,4), constrained_layout=True)
for [ax,cmap] in zip(axis,colormap):
a = ax.pcolormesh(x,cmap=cmap, rasterized=True, vmin=-5, vmax=5)
fig.colorbar(a, ax=ax)
plt.show()
x= cm.get_cmap('Oranges_r', 135)
y= cm.get_cmap('Blues', 135)
z = np.vstack((x(np.linspace(0, 1, 135)),
y(np.linspace(0, 1, 135))))
newcmp = ListedColormap(z, name='OrangeBlue')
plot([a, newcmp])
Import a numpy, matplotlib library. From matplotlib importing cm and listedcolormap. Getting a named Colormap. The second argument is for the size of the list of colors. Creating a function named Colormap. Giving the size of the colormaps. Next, giving the size of the figure as 8,4. Create for loop. Plot the custom color map using matplotlib.
Output
Brown Colormap to white
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap
a = cm.get_cmap('winter',8)
a = cm.get_cmap('winter',64)
colors = a(np.linspace(0,1,64))
red = np.array([250/265, 30/265, 144/265, 1])
colors[:25, :] = red
newcmp = ListedColormap(colors)
def plot(colormap):
np.random.seed(194201)
x= np.random.randn(50,50)
fig, axis = plt.subplots(1, 2, figsize=(8,4), constrained_layout=True)
for [ax,cmap] in zip(axis,colormap):
a = ax.pcolormesh(x,cmap=cmap, rasterized=True, vmin=-5, vmax=5)
fig.colorbar(a, ax=ax)
plt.show()
N = 265
vals = np.ones((N,3))
vals[:, 0] = np.linspace(100/265, 1, N)
vals[:, 1] = np.linspace(49/265, 1, N)
vals[:, 2] = np.linspace(51/265, 1, N)
newcmp = ListedColormap(vals)
plot([a, newcmp])
Import a numpy, matplotlib library. From matplotlib importing cm and listedcolormap. Getting a named Colormap. The second argument is for the size of the list of colors. Creating a function named Colormap. Giving the size of the colormaps. Next, giving the size of the figure as 8,4. Create for loop. Plot the custom color map using matplotlib.
Output
Colormap between Blue and green color
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
x = np.arange(0, 2 * 3.14, 0.1)
y = np.arange(0, 2 * 3.14, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.cos(X) * np.sin(Y) * 10
cdict = {'blue': ((0.0, 0.0, 0.0),
(0.5, 0.0, 0.1),
(1.0, 1.0, 1.0)),
'red': ((0.0, 0.0, 0.0),
(1.0, 0.0, 0.0)),
'green': ((0.0, 0.0, 1.0),
(0.5, 0.1, 0.0),
(1.0, 0.0, 0.0))
}
blue_green1 = LinearSegmentedColormap('BlueGreen1', cdict)
fig, axs = plt.subplots(2,2, figsize=(9,9))
im1 = axs[0,0].imshow(Z, cmap=blue_green1)
fig.colorbar(im1, ax=axs[0, 0])
plt.show()
You can create four different colormaps here by adding cdict2, cdict3. Here we added one colormap, for example, and better understanding purpose.
Output
FAQs Related to custom colormaps using matplotlib
Matplotlib is a library useful to create colormaps.
When we are using any plot or any visualization, that is going to require a colormap.
Conclusion
So far, we have completely learned about custom colormaps using matplotlib. Colormaps is one of the necessary things that you have to learn. Try to learn this article completely. It will be beneficial for you.
We hope you can understand about custom colormap using matplotlib. In case of any queries, do let us know in the comment section. We will be here to help you. Learn with us. Try to code on your own.