Modified by SunJackson

When Graphic Designers Meet Matplotlib – Creating Colormaps using Matplotlib in Python

Become a Certified Business Analytics Professional with 12+ Real-Life Projects, 1:1 Mentorship| Download Brochure Now

Blog

Blog Archive

Learn everything about Analytics

**

**

Blog

Blog Archive

SAS Business Analyst

LeaRn Data Science on R

Data Science in Python

DATA SCIENCE IN WEKA

Data Visualization with Tableau

Data Visualization with QlikView

Interactive Data Stories with D3.js

Applied Machine Learning Beginner to Professional

INTRODUCTION TO DATA SCIENCE

Natural Language Processing (NLP) Using Python

Computer Vision using Deep Learning 2.0

More Courses

When Graphic Designers Meet Matplotlib Creating Colormaps using Matplotlib in Python

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

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.

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-

You will get the result like this.viridis

Modifying Colormaps

Now, to modify the colormaps, you need to import these following sublibraries in Matplotlib.

To modify the number of color class in your colormaps, you can use this code

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.

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.

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

and red colormaps

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.

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.

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.

Related Articles

Tags : Cyclic colormaps, Diverging colormaps, Qualitative colormaps, Sequential colormaps

**Next ArticleStart Using The timeit Library in Python! ** **Previous Article10 Adventurous AI-Based TV Shows You Should Watch Next

Start Using The timeit Library in Python!

Guest Blog

45 Questions to test a data scientist on basics of Deep Learning (along with solution)

40 Questions to test a data scientist on Machine Learning [Solution: SkillPower – Machine Learning, DataFest 2017]

10 Powerful YouTube Channels for Data Science Aspirants!

Commonly used Machine Learning Algorithms (with Python and R Codes)

6 Top Tools for Analytics and Business Intelligence in 2020

40 Questions to test a Data Scientist on Clustering Techniques (Skill test Solution)

Web Scraping using Selenium with Python!

Basic Concepts of Object-Oriented Programming in Python

Recent Posts

Start Using The timeit Library in Python!

When Graphic Designers Meet Matplotlib Creating Colormaps using Matplotlib in Python

10 Adventurous AI-Based TV Shows You Should Watch Next

Busted! 11 Data Science Myths You Should Avoid at All Costs

Download App

Analytics Vidhya About Us Our Team Careers Contact us

Data Science Blog Hackathon Discussions Apply Jobs

Companies Post Jobs Trainings Hiring Hackathons Advertising

Visit us

**

**

**

**

© Copyright 2013-2020 Analytics Vidhya

Privacy Policy Terms of Use Refund Policy

**

Data Science

    标签: