Heatmap is a graphical representation of numerical data where values are expressed in colors. Plotting matplotlib heatmap provides you an effective summary of the data by converting the data values to pictorial representation. In this entire tutorial, you will know how to plot matplotlib heatmap in python with various methods.
Methods on plotting Matplotlib Heatmap in Python
In this section, you will know all the examples of Matplotlib heatmap. Please make sure that for more understanding do all the coding part in the Jupyter Notebook as I am also doing all the examples on it. In our examples we are using three python packages Numpy, Matplotlib, and Seaborn. Therefore make sure that you have installed it on Notebook. If you have not then install it using the below commands.
!pip install numpy !pip install matplotlib !pip install seaborn
Method 1 : Matplotlib Heatmap using imshow() function
In Matplotlib there is function plt.imshow() that accepts cmap argument. It allows you to plot a heatmap for your input data. Execute the lines of code to plot a heatmap on the sample data.
import matplotlib.pyplot as plt import numpy as np data= np.random.random((10, 10)) plt.imshow(data, cmap='hot', interpolation='nearest') plt.show()
Explanation of the code
Here I am first creating random data values using the numpy.random.rand() method. It will create 1o rows and 1o columns with random values. For plotting the heatmap the method plt.imshow() accepts data, cmap, and interpolation parameter. The first parameter(data) is the sample data points you have created. The second parameter cmap allows you to map scalar data to colors. The last parameter interpolation=’nearest’ simply displays an image without trying to interpolate between pixels If the display resolution is not the same as the image resolution. It results an image in which pixels are displayed as a square of multiple pixels.
When you will run the code you will get the following output.
Method 2: Matplotlib Heatmap using seaborn Package
The other method to plot the heatmap is using Matplotlib with the seaborn package. Seaborn is also a data visualization package build above on the Matplotlib. It draws a beautiful heatmap with a color map also. Execute the below lines of code to plot the heatmap.
import numpy as np import seaborn as sns import matplotlib.pylab as plt uniform_data = np.random.rand(10, 10) ax = sns.heatmap(uniform_data, linewidth=0.5) plt.show()
Explanation of the code
I have created the same random data points using the np.random.rand(10, 10). After that passes it to sns.heatmap() method. When you run the above code you will get the following output.
Heatmap is very useful for presenting the numerical data points in a graphical way. It tells you what numbers occur most in the dataset. Any number that occurrence is more than others then the color of that number is a dark color. These are the methods to plot Heatmap in Python. I hope you are now able to use heatmap in Matplotlib. If you have any queries then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.