Python

# Matplotlib Colorbar

Matplotlib is a graphing package for Python language using NumPy, the arithmetical extension. It offers an object-oriented API for inserting graphs into applications utilizing different GUI toolkits. There is also a procedural “pylab” interface built on a state machine (like OpenGL) that is meant to look like MATLAB, however, it is not recommended.

They are plotted on a separate axis in Matplotlib. Figure.colorbar or its pyplot covering pyplot.colorbar, which uses make_axes and colorbar internally, is commonly used to produce colorabars. You won’t have to manually invoke the approaches or initialize the modules in this segment as an end-user.

Matplotlib is a numerical-mathematical expansion for the NumPy library in Python. The top-level Artist, the Figure, is provided by the figure module, and it comprises all plot elements. The default spacing of subplots and the top plot elements are controlled by this module.

In this article, we will examine the methods to operate the Matplotlib Colorbar technique and how it can assist us to get the outcomes we want. Let’s begin the article with an easy illustration and further, we will discuss some more complex ones.

## Add a Vertical Colorbar to a Scatterplot

A normal probability plot of an ecommerce web page is shown below. It displays the proportion of viewers vs. the total of transactions. The ratio here between both is shown by the colorbar in this case. We could see from the colorbar indicating that the number of individuals is lesser on some days, transactions have been more.

Maximum conversion rates are denoted by the yellow dots. We could get a simple illustration of a vertical colorbar in the image below.

Now, we will look at the program’s code. We proceeded with the program by importing the Python Matplotlib library. Then for the Horizontal and Vertical directions, we provided different coordinates.

After that, we defined the conversion factor. Then we mapped it out like this. We are using the term cmap, which refers to colormap. The color related to the stated condition is created by the cmap.

import matplotlib.pyplot as plt

import numpy as num

Numofvisit = [3110, 920, 874, 3406, 4178, 2024, 4985]

sales = [350, 93, 68, 292, 439, 350, 180]

conversion = [.05,.09,.02,1.1,2.9,.37,.68]

plt.scatter(x=Numofvisit,y=sales,c=conversion,cmap="spring")

cbar=plt.colorbar(label="conversion", orientation="horizontal",shrink=.55)

cbar.set_ticks([2.14,.80, 0.35, 2.32, 1.8,1.0])

cbar.set_ticklabels(["x","x", "y", "z", "k","l"])

plt.show() We have been using the title component for the colorbar label, which indicates just what symbolizes or how it should be positioned. Here, the colorbar has the title ‘CONVERSION’ owing to the label tag. We utilized the ‘shrink’ feature to adjust the size of the given colorbar. The marks on the colorbar also have been applied here. We utilized the ‘set ticks’ and ‘set ticklabels’ methods to accomplish this. Tags are the phrase that shows along the dimension of the plot. However, we may easily change and modify these graphs to meet our requirements. We could also adjust the size, color, and style of the font.

## Add a Single Colorbar to Multiple Subplots

The first method is similar to traditional plotting in that involves first creating the main plot and then, adding a colorbar. In Matplotlib, there are two ways to add a colorbar: explicit and implicit. The purpose is to manually change the current axes in the stated technique to make room for an extra colorbar. Then, in the colorbar’s place, specifically, add an axis.

import matplotlib.pyplot as plt

import numpy as np

fig, axes = plt.subplots(nrows=3, ncols=4, figsize=(9.5, 6))

for ax in axes.flat:

ax.set_axis_off()

im = ax.imshow(np.random.random((14, 14)), cmap='spring',

vmin=0, vmax=1)

wspace=0.04, hspace=0.04)

cb_ax = fig.add_axes([0.9, 0.2, 0.04, 1.0])

cbar = fig.colorbar(im, cax=cb_ax)

cbar.set_ticks(np.arange(1, 1.2, 1.6))

plt.show() As we could modify the location of the defined colorbar accurately. The output image looks like this: ## Use of figure.colorbar Function

Matplotlib, on the other hand, includes an implicit function for replacing the original axes and allocating accommodation for a colorbar. The subsequent instance will assist us in comprehending this concept.

import matplotlib.pyplot as plt

import numpy as np

fig, axes = plt.subplots(nrows=3, ncols=4, figsize=(9.5, 6))

for ax in axes.flat:

ax.set_axis_off()

im = ax.imshow(np.random.random((14, 14)), cmap='spring',

vmin=0, vmax=1)

cbar = fig.colorbar(im, ax=axes.ravel().tolist(), shrink=0.89)

cbar.set_ticks(np.arange( 1.2, 1.6))

plt.show() To create the graph with different colorbar that seem identical height, manually adjust the shrink param of the fig.colorbar function. Notice that instead of using the cax param as in the previous example, we use the ax param of the figure.colorbar function. ## Colorbar for Multiple Plots

We might get an illustration of a colorbar with several graphs here. We will need a NumPy library as well as Matplotlib to run it. We would like to have four separate subplots here. Similarly, if we want to make six plots, we could utilize 2, 3, and 3, 2.

Following that, we utilized Matplotlib’s imshow method. Imshow is a technique that enables users to access a two-dimensional graphic as an output. NumPy’s random function is included in the imshow function. It gives numerous float values in between different open intervals [2, 1.5]. We are using it inside the “for loop” to execute it several times.

Vmin and Vmax are utilized to determine the spectrum of the “colorbar.” We could change it to fulfill our requirements. This indicates the colorbar’s frequency. The colorbar and display functionalities were then implemented. ## Conclusion

The Matplotlib Colorbar is explained in this article. Aside from that, we examined the structure and arguments. We examined a couple of instances to help us understand the Matplotlib colorbar. For every example, we changed the syntax and analyzed the output. Furthermore, we may determine that the Matplotlib Colorbar method is being utilized to create colorbars, which are a graphic illustration of multidimensional data. A colorbar represents the mapping of numeric attributes to colors in Matplotlib. This enables you to display your data in such a way that is accessible to a wide range of users. 