Python Heat Maps
We offer you a brighter future with FREE online courses - Start Now!!
Heat maps in Python is a type of a graph which represents different shades of a colour to distinguish the values in the graph. The higher values are represented in the darker shades and the lesser values are represented in lighter shades. There can also be a different colour in the graph when the value is more different from the other data values. The graphical representation of the values through color differentiation is known as heat map.
Heatmap is also called a shading matrix. The heatmap can also include normalizing the matrices, performing and analyzing the clusters, customizing the colors and permuting the rows and columns so that the user can place similar values nearby to each other.
Why use heatmap in Python?
Usually the users use the seaborn heatmap to visualize the data to get the insights for business or any other research purposes. When the data is huge, we usually tend to go for data visualization as it is one of the best techniques to understand the numerics and heatmap is one among them.
Seaborn Package in Python
Heatmaps are plotted using the seaborn package.
Syntax for Python Seaborn package
seaborn.heatmap(data, vmin=None, vmax=None, cmap=None, center=None, annot_kws=None, linewidths=0, linecolor = ‘black’, cbar=True)
Parameters of seaborn.heatmap syntax:
Parameters | Description |
Data | The dataset of the heatmap and it is one of the required parameters in the syntax. |
Vmin, vmax | These are the values for the colormap and these values are optional. |
cmap | Mapping of the data values to the color space and it is one of the optional parameters. |
center | This value represents where to center the color map while plotting the divergent data value. This is also an optional parameter. |
annot_kws | It is one of the optional parameters and if this value is set to true, the data value is written in each cell or if the value is set to false, the data value is not written in each cell. |
linewidths | The user uses this parameter to set the width of the line and it is an optional parameter. |
linecolor | Line color is the color which represents the color of line between each cell. |
Cbar | If it is set to true, then it represents the colorbar in the graph or else the colorbar is not displayed. |
How to create Python heatmap?
1. Create Simple Heat maps in Python
The following code below is a simple example of a heatmap.
Code to create a simple heatmap :
import numpy as np import seaborn as sn import matplotlib.pyplot as plt # generating 2D matrix of random numbers between 10 and 100 PythonGeeks = np.random.randint(low = 10, high = 100, size = (5, 5)) print("PythonGeeks Data \n") print(PythonGeeks) hm = sn.heatmap(data = PythonGeeks) # Heatmap Display plt.show()
Note: The user can choose to plot the python heatmap for discrete data.
Output of simple heatmap:
2. Annotating the heatmap in Python:
The user can add the annotation to each and every cell in heatmap.
Code:
heatmap = sn.heatmap(data=PythonGeeks, cmap="plasma", center = 0 , annot = True)
3. Adding gridlines in Python heat map:
The user can also add gridlines in the graph if they want in the heatmap.
Code:
heatmap = sn.heatmap(data=PythonGeeks, cmap="plasma", center = 0 , annot = True, linewidths = 5, linecolor = "Black")
4. Removing the labels in Python Heatmaps:
The user can also remove the xlabel and ylabel from the graph by setting the parameters “false”.
Code:
heatmap = sn.heatmap(data=PythonGeeks, cmap="plasma", center = 0 , linewidths = 3, linecolor = "Black", cbar = False, xticklabels = False, yticklabels = False)
5. Removing heat map colorbar scale
If the user does not wish to see the colorbar scale along with the graph, then they can remove that also.
Code:
heatmap = sn.heatmap(data=PythonGeeks, cmap="plasma", center = 0 , linewidths = 3, linecolor = "Black", cbar = False, xticklabels = False, yticklabels = False)
6. Removing the extra labels from heatmaps
If the user wants to remove unnecessary labels, then they can remove those labels from the heatmap.
Code:
Heatmap = sn.heatmap(data=PythonGeeks, cmap="plasma", center = 0 , linewidths = 3, linecolor = "Black", cbar = False, xticklabels = 3, yticklabels = False)
7. Customizing the color theme in Python Heat maps
The user can choose a single color also to the heatmap in python.
Code:
heatmap = sn.heatmap(data=PythonGeeks, cmap="pink")
8. Normalizing a column in the data
If a data in the column has high values, then those data can also be normalised using the normalization formula in the code. After normalising the data, we can again build the heatmap.
Here, in the “code 1”, we are going to add a piece of code to increase the values in the 3rd column in the data.
Code 1:
import pandas as pd df=pd.DataFrame(np.random.randn(10,10)*5+5) df[3]=df[1]+50 sn.heatmap(df,cmap='YlGnBu')
Code 2: After normalizing the data
df_normalization=(df-df.mean())/df.std() sn.heatmap(df_normalization,cmap='YlGnBu')
Anchoring the colormap in Python Heatmap:
Anchoring the colormap means that when the user sets the vmin and vmax value then only the cells between those cells will be displayed on the graph.
For example, if we are setting the vmin and vmax values as 10 and 80, then the values below 10 and after 80 are ignored.
Code to anchor the colormap:
import numpy as np import seaborn as sn import matplotlib.pyplot as plt PythonGeeks = np.random.randint(low=10, high=100, size=(10, 10)) # plotting the heatmap hm = sn.heatmap(data=PythonGeeks, vmin=10, vmax=80) # displaying the plotted heatmap plt.show()
Output of the code to anchor the colormap:
Choosing the colormap:
There are multiple colormaps in the matplotlib package. In our example, we will be using “prism” as our cmap.
Code to choose the colormap:
import numpy as np import seaborn as sn import matplotlib.pyplot as plt PythonGeeks = np.random.randint(low=10, high=100, size=(10, 10)) heatmap = sn.heatmap(data=PythonGeeks, cmap="prism") plt.show()
Choose the colormap output:
Centering the colormap:
The user can also center the colormap by passing the center parameter as 0.
Code to center the color map:
import numpy as np import seaborn as sn import matplotlib.pyplot as plt PythonGeeks = np.random.randint(low=10, high=100, size=(10, 10)) heatmap = sn.heatmap(data=PythonGeeks, cmap="plasma", center = 0) plt.show()
Center the colormap output:
Displaying the cell values in the graph
If the user wants to display the data values then the parameter “annot” in the syntax must be true. The parameter “fmt” is generally used to choose the datatype of the cell contents.
Code to display the cell values
import numpy as np import seaborn as sn import matplotlib.pyplot as plt PythonGeeks = np.random.randint(low=10, high=100, size=(10, 10)) heatmap = sn.heatmap(data=PythonGeeks, cmap="plasma", center = 0 , annot = True) plt.show()
Displaying the cell values output:
Customizing the lines in the graph
The user can customize the color and thickness of the lines separating the cells. We can customize it using the ‘linecolor’ and ‘linewidth’ parameters.
Code to customize the lines
import numpy as np import seaborn as sn import matplotlib.pyplot as plt PythonGeeks = np.random.randint(low=10, high=100, size=(10, 10)) heatmap = sn.heatmap(data=PythonGeeks, cmap="plasma", center = 0 , annot = True, linewidths = 5, linecolor = "Black") plt.show()
Customizing the lines output:
Hiding the colorbar
The user can disable the colorbar from the graph. We can do this by setting the ‘cbar’ parameter to false.
Code to hide the color bar:
import numpy as np import seaborn as sn import matplotlib.pyplot as plt PythonGeeks = np.random.randint(low=10, high=100, size=(10, 10)) heatmap = sn.heatmap(data=PythonGeeks, cmap="plasma", center = 0 , annot = True, linewidths = 5, linecolor = "Black", cbar = False) plt.show()
Hiding the color bar output:
Removing the Labels
The user can also remove the x label and y label axis in the graph by passing the ‘xticklabels’ and ‘yticklabels’ parameter as false.
Code to remove the labels from the graph:
import numpy as np import seaborn as sn import matplotlib.pyplot as plt PythonGeeks = np.random.randint(low=10, high=100, size=(10, 10)) heatmap = sn.heatmap(data=PythonGeeks, cmap="plasma", center = 0 , linewidths = 3, linecolor = "Black", cbar = False, xticklabels = False, yticklabels = False) plt.show()
Removing the labels output:
Heatmap using library pandas:
Now using library pandas, we are going to develop the heatmap and we are also going to map the indices and columns of the two-dimensional plot figure.
Code to develop heat map using pandas:
from pandas import DataFrame import matplotlib.pyplot as plt dataa=[{12,13,14,11},{16,13,15,12},{16,13,15,14},{13,17,15,14},{12,18,11,15}] Ind= ['I1', 'I2','I3','I4','I5'] Col = ['C1', 'C2', 'C3','C4'] df = DataFrame(dataa, index=Ind, columns=Col) plt.pcolor(df) plt.show()
Heatmap using Pandas output:
Summary:
In this tutorial, we have learnt how to create a heatmap using the seaborn package and with the assistance of the different parameters we have also learnt how to customize the heatmap as per our requirement.