Customized Colorbars Using Matplotlib Pyplot

This tutorial is made to give you in-depth knowledge of the ways by which we can customize the colorbar using the matplotlib pyplot library. Matplotlib pyplot is a python library specially designed to create or plot various graphs in python to read different types of data structures graphically.

To make the plot look more catchy and presentable to our eyes we use the colormaps to color the plot points or the plot lines. There are various colormaps that we can use to make our plots look more colorful and easy to understand. A colorbar is used to give the scale of colors, which is mentioned in the form of numbers, ie… which number depicts which shade of the color just for the reference of the user who is reading the graph.

Matplotlib pyplot

Make sure your code space has the matplotlib library preinstalled in it or you may install it using the following instruction.

from matplotlib import pyplot as plt
import matplotlib.pyplot as plt

Methods of customizing colorbar


The orientation of the colorbar can be done as horizontal or vertical using the following lines of code.

# for horizontal orientation
#for vertical orientation

This picture depicts a horizontal orientation:

depicts a horizontal orientation

This picture depicts a vertical orientation:

depicts a vertical orientation

Adding a label to the colorbar:

plt.colorbar(label="an example")

label to the colorbar Matplotlib

Setting limits of the colorbar:

The function below is used to set the maximum and minimum values of the colorbar.


Here minimum value is 0 and the maximum value is 3.

Extending both ends of the colorbar:

This will add in little triangles at the top and bottom of the colorbar and so any data which is outside the range you have selected will then be represented by the colors that are inside these little triangles.


Extending both ends of the colorbar Matplotlib

Discrete colors of colorbar:

You can also have discrete colors rather than a continuous sliding scale. To do that you need to make the following changes.


Here “viridis” is a colormap and 5 is the number of shades.

Pad, shrink, aspect, and format of the colorbar:

Padding of the colorbar changes the distance of the colorbar from the main graph and shrinking reduces and increases the height of the colorbar. If the pad is set to a lower numeric value the colorbar comes very close to the main graph and vice-versa. Similarly, if shrink is set to a lower numeric value the height of the colorbar reduces and vice-versa. Aspect is used to customize the thickness of the colorbar, lower the aspect thicker than the colorbar, and vice-versa. The format is used to format the readings on the colorbar.


Matplotlib Pad, shrink, aspect, and format of the colorbar

Customizing the ticks on the colorbar:

we can set the tick marks of our choice on the colorbar and also the ticklabels on the colorbar using the following line of code.

#entire code for reference
from matplotlib import pyplot as plt

#for setting ticks
#for setting tick labels

Matplotlib Customizing the ticks on the colorbar

Leave a Reply

Your email address will not be published. Required fields are marked *