How to Change Matplotlib Color Bar Size in Python?


Visualization tools are a vital part of Matplotlib library. One of the tools is colorbar. It shows the mapping between data values and colors in a plot. For adjusting the size of the colorbar to make it more visible or to fit it better with the plot we have several parameters presented by the colorbar() function in Matplotlib.

In this article, we will discuss how to change matplotlib colorbar size using different approaches.

What is Matplotlib and How to Install it?

Matplotlib is a widespread library for creating static, animated and interactive visualization in python. It has module name “pyplot” which makes things easy for plotting by providing features to control line-styles, font properties, formatting axes, etc.

We can install matplotlib by using the following command –

pip install matplotlib

What is pyplot?

Pyplot is a sub-library of the Matplotlib library. It is commonly used in data analysis and visualization tasks, as it helps user to easily change the appearance of their plots using different parameters. It is a simple way of creating complex graphs within less code. It gives you various options such as different colors, titles, line styles, allows you to add text, images, etc to your plot.

Colorbar in Matplotlib?

Colorbars are a visualization of the mapping from scalar values. A colorbar is the bar that consists of various colors in it placed along the sides of the matplotlib chart. It is the legend for colors shown in the chart. By default, the position of the matplotlib color bar is on the right side of matplotlib chart.

A color map in colour bar consists of five colors in it. And “rainbow” colormap in colorbar have seven colours in it.

The colorbar() function in pyplot module of matplotlib is used to create colorbar to a plot indicating the color scale. We can change the orientation of colorbar and it can be horizontal or, vertical.

Example

In this plot we have created vertical colorbar, in a scatter graph using Matplotlib. Two list “items” and “sales” are named and then a plot is created using plt.scatter() function for the scatter graph.

X-axis titled as “ITEMS” and y–axis titled as “SALES”, and titled whole graph as “SHOP MANGEMENT”. For creating colorbar in a plot we have used a plt.colorbar() function.

import matplotlib.pyplot as plt
items=["chocolate","toffees","cake","colddrinks","lays","biscuit","frooti"]
sales=[89000,23000,98777,12000,65000,11100,96000]
c=['r','y','m','r','y','m','b']
a=plt.scatter(items,sales,color=c)
plt.xlabel("ITEMS")
plt.ylabel("SALES")
plt.suptitle("SHOP MANGEMENT",fontsize=20)
plt.colorbar()
plt.show()

Output

Example

In this plot we have created horizontal colorbar by using orientation=”horizontal” in plt.colorbar function of the Matplotlib.

import matplotlib.pyplot as plt
items=["chocolate","toffees","cake","colddrinks","lays","biscuit","frooti"]
sales =[89000,23000,98777,12000,65000,11100,96000]
c= ['r','y','m','r','y','m','b']
a=plt.scatter(items, sales, color=c)
plt.xlabel("ITEMS")
plt.ylabel("SALES")
plt.suptitle("SHOP MANGEMENT", fontsize=20)
plt.colorbar(orientation="horizontal")
plt.show()

Output

Colorbar to a Heat Map

In the similar way you can add colorbar to different plots. Let’s take an example of a heatmap plot. We can create a 2D array of random values using NumPy rand() function.

Example

import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(4, 4)
heatmap = plt.imshow(data, cmap='Greens')
colorbar = plt.colorbar(heatmap)
plt.show()

Output

Changing the Size of Colorbar in Matplotlib?

We can change the size of the colorbar by passing value to the shrink parameter of the colorbar() function. The shrink value should be less than 1 if you need to decrease the size of the colorbar. Depending on the value you pass the size is reduced. If you pass 0.5 as a value to this parameter, the size of the colorbar is reduced to half.

Example

Here, we have plotted a scatter graph using plot matplotlib. For the graph we have created two lists “items” and “sales” and then created a scatter graph using plt.scatter() statement. We have created a colorbar with the shring value 0.5.

import matplotlib.pyplot as plt
items=["chocolate","toffees","cake","colddrinks","lays","biscuit","frooti"]
sales=[89000,23000,98777,12000,65000,11100,96000]
c= ['r','y','m','r','y','m','b']
a= plt.scatter(items, sales, color=c)
plt.xlabel("ITEMS")
plt.ylabel("SALES")
plt.suptitle("SHOP MANGEMENT",fontsize=20)
plt.colorbar(a, shrink=0.5)
plt.show()

Output

Following will be the output when the shrink size is 2 –

You can also dynamically change the size of a colorbar in Matplotlib, write a function which will work in a way such that it will take the current size of the colorbar as the input value and return a new size based on the external factors such as amount of data, size of the plot.

Conclusion

In this article, we have discussed about what colorbar is, how it is created and how to change the size of colorbar in matplotlib using variety of examples.

Updated on: 11-Oct-2023

508 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements