How to add matplotlib colormaps in Python ? Stepwise Implementation

How to add matplotlib colormaps in Python

We can add matplotlib colormaps in python using colorbar() function. This colorbar() function belongs to matplotlib.pyplot python package. In this article, We will see a minimal example of add matplotlib colormaps in Python.

 

Adding  matplotlib colormaps : Stepwise –

In this section,  We will use matplotlib colormaps stepwise. At the end of these steps, We will combine them and execute together.

Step 1: Importing Packages for colormaps-

Firstly, In order to use and demonstrate of colorbar() function, We need to import the below module.

import matplotlib.pyplot as plt
import numpy as np

matplotlib.pyplot is for colorbar() function and numpy for data creation.

Step 2: Datapoint creation –

Secondly in this section, We will create some data points just for demonstration. In real scenarios, We have data points already in the picture. In order to create some data points, we will use NumPy array. Here is the code for that.

sample = np.random.random([10, 20]) 

It will create a sample NumPy array with dimension 10*20.

Step 3: Invoking colorbar() function –

Before invoking the colorbar() function, We need to give dimension for figsize and pcolormesh(). Here is the easiest way for that.

plot.figure(figsize=(10, 12))
plot.pcolormesh(sample)
plot.colorbar()

Complete Code with output-

Further, If we merge the code from each steps and run it. We get the below output.

import numpy as np
import matplotlib.pyplot as plot
sample = np.random.random([10, 20]) 
plot.figure(figsize=(10, 12))
plot.pcolormesh(sample)
plot.colorbar()
matplotlib colormaps in Python
matplotlib colormaps in Python

How to change default colormap type?

See, The default colormap is  ‘viridis’ . But we can parameterize it as cmap in pcolormesh() function. Lets see the syntax.

plot.pcolormesh(sample, cmap='inferno')

Here we have changed the colormap from viridis to inferno. Let’s run it with the above cmap example.

import numpy as np
import matplotlib.pyplot as plot
sample = np.random.random([10, 20]) 
plot.figure(figsize=(10, 12))
plot.pcolormesh(sample, cmap='inferno')
plot.colorbar()
colormaps in Python
colormaps in Python

 

Conclusion –

colormaps are very interactive in nature.  I hope this article, Must have cleared the concept of using matplotlib colormaps in Python.  We have also given a minimal example of this. I hope most of the thing a self-explanatory in nature. Still, If you want to ask something else related to this topic, please comment below.

 

Thanks

Data Science Learner Team

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner