A heatmap is a graphical representation of data where values are depicted by color. They make it easy to understand complex data at a glance. Heatmaps can be easily drawn using seaborn in python. In this article, we are going to add a frame to a seaborn heatmap figure in Python.
Syntax: seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, annot_kws=None, linewidths=0, linecolor=’white’, cbar=True, **kwargs)
Important Parameters:
- data: 2D dataset that can be coerced into an ndarray.
- linewidths: Width of the lines that will divide each cell.
- linecolor: Color of the lines that will divide each cell.
- cbar: Whether to draw a colorbar.
All the parameters except data are optional.
Returns: An object of type matplotlib.axes._subplots.AxesSubplot
Create a heatmap
To draw the heatmap we will use the in-built data set of seaborn. Seaborn has many in-built data sets like titanic.csv, penguins.csv, flights.csv, exercise.csv. We can also make our data set it should just be a rectangular ndarray.
Python3
import seaborn as sns
import matplotlib.pyplot as plt
example = sns.load_dataset( "flights" )
example = example.pivot( "month" , "year" ,
"passengers" )
res = sns.heatmap(example)
plt.show()
|
Output:

basic heatmap
There are two ways of drawing the frame around a heatmap:
- Using axhline and axvline.
- Using spines (more optimal)
Method 1: Using axhline and axvline
The Axes.axhline() and Axes.axvline() function in axes module of matplotlib library is used to add a horizontal and vertical line across the axis respectively.
We can draw two horizontal lines from y=0 and from y= number of rows in our dataset and it will draw a frame covering two sides of our heatmap. Then we can draw two vertical lines from x=0 and x=number of columns in our dataset and it will draw a frame covering the remaining two sides so our heatmap will have a complete frame.
Note: It is not an optimal way to draw a frame as when we increase the line width is does not consider when it is overlapping the heatmap.
Example 1.
Python3
import seaborn as sns
import matplotlib.pyplot as plt
example = sns.load_dataset( "flights" )
example = example.pivot( "month" , "year" ,
"passengers" )
res = sns.heatmap(example, cmap = "BuPu" )
res.axhline(y = 0 , color = 'k' ,linewidth = 10 )
res.axhline(y = example.shape[ 1 ], color = 'k' ,
linewidth = 10 )
res.axvline(x = 0 , color = 'k' ,
linewidth = 10 )
res.axvline(x = example.shape[ 0 ],
color = 'k' , linewidth = 10 )
plt.show()
|
Output:

Example 2:
Python3
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
example = np.random.rand( 10 , 12 )
res = sns.heatmap(example, cmap = "magma" ,
linewidths = 0.5 )
res.axhline(y = 0 , color = 'k' ,
linewidth = 15 )
res.axhline(y = 10 , color = 'k' ,
linewidth = 15 )
res.axvline(x = 0 , color = 'k' ,
linewidth = 15 )
res.axvline(x = 12 , color = 'k' ,
linewidth = 15 )
plt.show()
|
Output:

Method 2: Using spines
Spines are the lines connecting the axis tick marks and noting the boundaries of the data area. They can be placed at arbitrary positions.
Example 1:
width of the line can be changed using the set_linewidth parameter which accepts a float value as an argument.
Python3
import seaborn as sns
import matplotlib.pyplot as plt
example = sns.load_dataset( "flights" )
example = example.pivot( "month" , "year" ,
"passengers" )
res = sns.heatmap(example, cmap = "Purples" )
for _, spine in res.spines.items():
spine.set_visible( True )
spine.set_linewidth( 5 )
plt.show()
|
Output:

Example 2:
We can specify the style of the frame using the set_linestyle parameter of the spine(solid, dashed, dashdot, dotted).
Python3
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
example = np.random.rand( 10 , 12 )
res = sns.heatmap(example, cmap = "Greens" ,
linewidths = 2 ,
linecolor = "white" )
for _, spine in res.spines.items():
spine.set_visible( True )
spine.set_linewidth( 3 )
spine.set_linestyle( "dashdot" )
plt.show()
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
03 Jan, 2021
Like Article
Save Article