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
or
import matplotlib.pyplot as plt

Methods of customizing colorbar

Orientation:

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

# for horizontal orientation
plt.colorbar(orientation="horizontal")
#for vertical orientation
plt.colorbar(orientation="vertical")

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.

plt.clim(0,3)

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.

plt.colorbar(extend="both")

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.

plt.scatter(cmap=plt.cm.get_cmap("viridis",5)

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.

plt.colorbar(pad=0.01,shrink=0.5,aspect=20,format="%.4f")

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
x=[10,20,30,40,50]
y=[1,2,3,4,5]
color=[1,2,3,4,5]
plt.scatter(x,y,c=color,cmap=plt.cm.get_cmap("viridis",5))
cbar=plt.colorbar(orientation="vertical",extend="both",pad=0.1,shrink=0.8,format="%.4f")

#for setting ticks
cbar.set_ticks([1,2,5])
#for setting tick labels
cbar.set_ticklabels(["noise","dance","name"])
plt.show()

Matplotlib Customizing the ticks on the colorbar

Leave a Reply

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