How to Draw a Heatmap Plot

In this lesson, we will learn how to show the relationship between variables by using heatmap.

What is heatmap?

heatmap is a useful chart that we can use to show the relationship between two variables. Heatmap displays a general view of numerical data; it does not extract specific data points. It is a graphical representation of data where the individual values contained in a matrix are represented as colors.

In Matplotlib, there is no native function for creating a heatmap, like there is with pie() or scatter(). However, there is another function, imshow(), which can help us create an image with the heatmap effect.

Plotting heatmap from imshow()

In order to create a heatmap, we can pass a 2-D array to imshow(). As we can see in the code below, passing the values to imshow is the core operation of the plot.

After we pass the values, we can set the x-axis and y-axis labels by calling set_xticks and set_xticklabels.

values = np.array([[0.8, 1.2, 0.3, 0.9, 2.2],
                   [2.5, 0.1, 0.6, 1.6, 0.7],
                   [1.1, 1.3, 2.8, 0.5, 1.7],
                   [0.2, 1.2, 1.7, 2.2, 0.5],
                   [1.4, 0.7, 0.3, 1.8, 1.0]])

im = axe.imshow(values)

In the example code below, line 7 creates a 2D array from NumPy, which is considered part of the matrix. The values that we used are the correlation between two different variables. For ...