![]() |
VOOZH | about |
A heatmap is a great tool for visualizing data across the surface. It highlights data that have a higher or lower concentration in the data distribution. A 2-D Heatmap is a data visualization tool that helps to represent the magnitude of the matrix in form of a colored table. In Python, we can plot 2-D Heatmaps using the Matplotlib and Seaborn packages. There are different methods to plot 2-D Heatmaps, some of which are discussed below.
Letβs see an example: We will use the tips dataset which is an inbuilt dataset. This dataset contains information about restaurant tips, total bill amount, tip amount, customer details like sex and day of the week etc. Also, we will be using Seaborn and Matplotlib libraries for this.
Output
seaborn.heatmap(data, vmin=None, vmax=None, cmap=None, annot=None, fmt='.2g', linewidths=0, linecolor='white', cbar=True, square=False, **kwargs)
Parameters:
data: Rectangular dataset (2D array, DataFrame, or similar) used to draw the heatmap.vmin, vmax: Values to anchor the colormap, otherwise inferred from the data.cmap: Colormap used to map data values to colors (e.g., "coolwarm", "YlGnBu").annot: If True, write the data value in each cell; can also be a DataFrame of strings.fmt: String formatting code to use when adding annotations.linewidths: Width of the lines that will divide each cell.linecolor: Color of the lines dividing the cells.cbar: Boolean value to display the color bar.square: If True, set the Axes aspect to βequalβ so each cell will be square-shaped.Returns: It returns a matplotlib Axes object with the heatmap drawn onto it.
We can choose different colors for Heatmap using the cmap parameter. cmap can help us in making our heatmap more informative.
Output:
We can add a colorbar to the heatmap using plt.colorbar(). colorbar shows the weight of color relatively between a certain range.
Output:
We can customize this heatmap using different functions and parameters to make it more informative and beautiful. we will useplt.annotate() to annotate values in the heatmap. Also, we will use colors library to customize the color of the heatmap.
Output:
Next, we will use a heatmap to plot the correlation between columns of the dataset. We will use correlation to find the relation between columns of the dataset.
Output:
We can also use the Seaborn library to plot heatmaps even plotting heatmaps using Seaborn is comparatively easier than the matplotlib library. To plot the heatmap using Seaborn we will use sns.heatmap() function from the Seaborn library.
Output:
As we know the Heatmap is just a colored representation of a matrix. However, heatmap has a very large use case. We can use heatmaps for the following purpose.