Seaborn Cluster Map

Seaborn is a remarkable visualization module for Python that allows you to plot statistical visuals. It is based on the Matplotlib software and is tightly connected with Pandas’ data structures. In an unsupervised learning, clustering techniques aid in the acquisition of structured data. In this article, we’ll see what a cluster map is and how to construct and use this for a variety of purposes.

Syntax of the Cluster Map in Seaborn

We have a simple syntax for the Seaborn cluster map here:

seaborn.clustermap(data,, standard_scale=None, figsize=(6, 8), **kwargs)

Below, we explained the parameter passed inside the Seaborn cluster function along with some optional parameters.

data: For clustering, rectangular data is used. NAs aren’t allowed.

pivot_kws: If the data is in a tidy dataframe, you can use the keyword parameters to make a rectangular dataframe with a pivot.

method: To calculate clusters, apply the linkage approach. For further details, see the documentation for scipy.cluster.hierarchy.linkage().

metric: The data should be measured in terms of distance. More parameters can be found in the scipy.spatial.distance.pdist() documentation. You can create every linkage matrix manually and supply it as a row. Col linkage uses the metrics (or methodologies) for rows and columns.

z_score: Whether or not z-scores should be calculated for the columns or rows. Z scores are calculated as z = (x – mean)/std, which means that each row’s (column’s) values will be deducted from the row’s (column’s) mean, then split by the row’s (column’s) standard deviation (column). This guarantees an average of 0 and a variation of 1 for each row (column).

standard_scale: Whether or not to normalize that dimension, means subtracting the minimum and dividing each row or column by its maximum.

figsize: The figure’s overall size which includes width and the height.

{row, col}_cluster: If True, the rows and columns will be clustered together.

{row, col}_colors: The colors to label the rows or columns. It can be used to see if the data inside a collection is clustered collectively. For several color levels of labeling, you can use the stacked lists or a DataFrame if delivered in the form of a Panda. DataFrame or Pandas are both good options. Color labels are derived from the DataFrames field names or the Series name. The colors in the DataFrame/Series are also correlated to the dataset by index, ensuring that the colors are presented in the proper sequence.

{dendrogram, colors}_ratio: The percentage of the graphic size is dedicated to the two border sections. When a pair is specified, it refers to the row and col ratios.

cbar_pos: In the diagram, the colorbar axes are in the correct positions. The colorbar is turned off if you set it to None.

kwargs: Heatmap receives all of the other keyword parameters().

We will construct a heat map using the hierarchical clusters through the Seaborn’s Clustermap function. Seaborn’s Clustermap is a really useful function. We’ll show you how to utilize it with some examples:

Example 1:

The cluster map of the Seaborn is a matrix graphic that allows you to visualize your matrix elements as a heat map while simultaneously displaying a clustering of your rows and columns. In the subsequent example, we brought in the required libraries. Then, we created a data frame of the employees which includes their names, ids, age, and salary.  We then converted this data frame into the Pandas by using the pd.dataframe function. We set the index of the Employee_data by the Name field through the set function.

After this, we created a cluster map of this data frame by calling the Seaborn cluster function and passing the Employee_data into that function. Another keyword argument, annot, is used, and is set to True. This parameter enables us to see the real numbers displayed on the cluster map’s heat map.

The output of the cluster map is in the following figure. Note that our rows and columns are rearranged by Seaborn:

Example 2:

Let’s use the sample dataset “mpg” to create a cluster map. We must filter the data we send to these cluster maps down to the number of columns in the data frame only.

Begin with importing the necessary libraries. We loaded the data set of “mpg” inside the “DataFrame_mpg” variable. Also, we used the dropna function to remove the null rows inside the data frame. We printed the column’s name inside the “mpg” dataframe along with the column size. Then, we have a cluster map function where the entire “mpg” dataframe is passed with the specified columns.

The three columns are shown in the console.

When we executed the previous code, we see a cluster map with only one column with a light color. This is because the scales for these several columns are different.

Example 3:

There are several options for scaling the data inside the cluster map function. But one simple method is to utilize the standard scale argument. If we want to scale each row, then we must pass a value of zero as an argument. If we want to scale each column, the value will be 1. Now, we have a scale value of 1. Also, we passed a method argument inside the cluster function which assigned a value as single.  The string can be passed as a single value, which is a minimal linkage.

The data frame “iris” cluster map is slightly different in the figure as we passed a scale and method parameters.

Example 4:

Here, we added the row_color parameter inside the Seaborn cluster map function. We assigned each color to the field species and pulled the information from the species column of the data frame penguins.


Now, you can establish the Seaborn cluster map since we explained it with some examples of the different parameters passed. Seaborn’s Clustermap also has a lot of alternatives for calculating a length or resemblance grid from the data to create a heatmap.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content