All About Heatmaps. The Comprehensive Guide | by Shrashti Singhal | Towards Data Science

1. What is a Heatmap
2. Uses
3. Types
4. Who uses HeatMaps
5. Choropleth VS HeatMap
6. Software Implementation
7. Color Schemes
8. Best Practices of using HeatMaps
9. Purpose of Seaborn HeatMap
10. Correlation Matrix
11. How to Read a Seaborn Heat map
12. Inputs for Heatmap
13. Heatmap Function
14. Create a default Heatmap using Seaborn
15. Seaborn Color Palletes
16. Seaborn Color Maps
17. Generating colormap from seaborn Palletes
18. Customizing Heatmap Style
19. Exploratory data analysis (EDA) with the heatmap
20. Advanced Heatmaps
Heatmaps visualize the data in a 2-dimensional format in the form of colored maps. The color maps use hue, saturation, or luminance to achieve color variation to display various details. This color variation gives visual cues to the readers about the magnitude of numeric values. HeatMaps is about replacing numbers with colors because the human brain understands visuals better than numbers, text, or any written data. Human beings are visual learners; therefore, visualizing the data in any form makes more sense. Heatmaps represent data in an easy-to-understand manner. Thus visualizing methods like HeatMaps have become popular.
Heatmaps can describe the density or intensity of variables, visualize patterns, variance, and even anomalies. Heatmaps show relationships between variables. These variables are plotted on both axes. We look for patterns in the cell by noticing the color change. It only accepts numeric data and plots it on the grid, displaying different data values by varying color intensity.
Business Analytics: A heat map is used as a visual business analytics tool. A heat map gives quick visual cues about the current results, performance, and scope for improvements. Heatmaps can analyze the existing data and find areas of intensity that might reflect where most customers reside, areas of risk of market saturation, or cold sites and sites that need a boost. Heat maps can be continued to be updated to reflect the growth and efforts. These maps can be integrated into a business’s workflow and become a part of ongoing analytics. Heat maps present the data in a visual and easy to understand manner to communicate to team members or clients.3
Website: Heatmaps are used in websites to visualize data of visitors’ behavior. This visualization helps business owners and marketers to identify the best & worst-performing sections of a webpage. These insights help them with optimization.
Exploratory Data Analysis: EDA is a task performed by data scientists to get familiar with the data. All the initial studies are done to understand the data are known as EDA. Exploratory Data Analysis (EDA) is the process of analyzing datasets before the modeling task. It is a tedious task to look at a spreadsheet filled with numbers and determine essential characteristics in a dataset. Therefore EDA is done to summarize their main features, often with visual methods, which includes Heatmaps. Heatmaps are a compelling way to visualize relationships between variables in high dimensional space. It can be done using feature variables as row headers and column headers, and the variable vs. itself on the diagonal.
Molecular Biology: Heat maps are used to study disparity and similarity patterns in DNA, RNA, etc.
Geovisualization: Geospatial heatmap charts are useful for displaying how geographical areas of a map are compared to one another based on specific criteria. Heatmaps help in cluster analysis or hotspot analysis to detect clusters of high concentrations of activity; For example, Airbnb rental price analysis.
Marketing and Sales: The heatmap’s capability to detect warm and cold spots is used to improve marketing response rates by targeted marketing. Heatmaps allow the detection of areas that respond to campaigns, under-served markets, customer residence, and high sale trends, which helps optimize product lineups, capitalize on sales, create targeted customer segments, and assess regional demographics.3
Typically, there are two types of Heatmaps:
Grid Heatmap: The magnitudes of values shown through colors are laid out into a matrix of rows and columns, mostly by a density-based function. Below are the types of Grid Heatmaps.
o Clustered Heatmap: The goal of Clustered Heatmap is to build associations between both the data points and their features. This type of heatmap implements clustering as part of the process of grouping similar features. Clustered Heatmaps are widely used in biological sciences for studying gene similarities across individuals.
The order of the rows in Clustered Heatmap is determined by performing hierarchical cluster analysis of the rows. Clustering positions similar rows together on the map. Similarly, the order of the columns is determined.
o Correlogram: A correlogram replaces each of the variables on the two axes with numeric variables in the dataset. Each square depicts the relationship between the two intersecting variables, which helps to build descriptive or predictive statistical models.
Spatial Heatmap: Each square in a Heatmap is assigned a color representation according to the nearby cells’ value. The location of color is according to the magnitude of the value in that particular space. These Heatmaps are data-driven “paint by numbers” canvas overlaid on top of an image. The cells with higher values than other cells are given a hot color, while cells with lower values are assigned a cold color.
Heat maps are used by any organization that collects and uses data to improve their finances, sales, operations, customer service, or marketing.
Some of the industries that use Heatmaps:3
Healthcare
Finance
Technology
Real estate
Choropleth maps are sometimes confused with heat maps. A choropleth map features different shading patterns within geographic boundaries to show the proportion of a variable of interest2. In contrast, a heat map does not correspond to geographic boundaries. Choropleth maps visualize the variability of a variable across a geographic area or within a region. A heat map uses regions drawn according to the variable’s pattern, rather than the a priori geographic areas of choropleth maps1. The Choropleth is aggregated into well-known geographic units, such as countries, states, provinces, and counties.
MATLAB provides heat map visualization with a wide variety of configuration options. Seaborn, a Python library built on top of the matplot python library, is used to draw custom heatmaps. Seaborn heatmap visualizes the correlation matrix, which helps in EDA, feature selection, and solving business problems.
R software environment can trace Heatmaps via the heatmaply R package.
Google Fusion Tables allow creating a heat map from a Google Sheets spreadsheet limited to 1000 points of geographic data.
Openlayers3 can plot a heat map layer of a selected property of all geographic features in a vector layer.
D3.js is a java library for data visualization that can create interactive heat map charts.
Gnuplot is a universal command-line plotting program that can trace 2D and 3D heat maps.
Tableau: Tableau Software is a data visualization software. Tableau is regarded as the grandmaster of data visualization software. It is suited to handle vast and fast-changing datasets. Its integration with advanced database solutions, such as Hadoop, Amazon AWS, My SQL, SAP, and Teradata, allows it to be used in Big Data operations, artificial intelligence, and machine learning applications.
Qlikview: Qlikview tool is the other major player in data visualization and Tableau’s biggest competitor. QlikView provides an in-the-box application development platform with inbuilt data models, layouts, and charts. The pre-built business applications for data analytics allows users to focus on exploring data and navigating information to drill down into particular details.
Qlik Sense: Qlik Sense is a business intelligence (BI) and visual analytics platform that supports a range of analytic use cases. It allows displaying comparative data as color patterns in a heat map included in its visualization bundle. Qlik Sense is an intuitive self-service data visualization and data discovery tool for creating visualizations and applications. Users are not restricted to pre-made charts.
FusionCharts: Fusion Charts is a JavaScript-based charting and visualization package. It can produce 90 different chart types. Users can generate quick visualizations by selecting from a wide range of templates rather than starting visualization from scratch and merely plugging in their data sources as needed.
Highcharts: Highcharts has cross-browser support, which enables anyone to view and run its interactive visualizations. It allows fast and flexible solution with a minimum data visualization training.
Datawrapper: Datawrapper has a simple and straightforward interface to upload CSV data and create clear charts and maps, which can be embedded in reports. It is a popular choice among media organizations that frequently use it to create charts and present statistics.
Plotly: Plotly is built on top of the open-source d3.js visualization library, enabling complex and sophisticated visualizations. It is integrated with analytics-oriented programming languages such as Python, R, and Matlab. It is user-friendly and provides inbuilt support for APIs such as Salesforce.
Sisense: Sisense has a drag and drop interface for charts, graphics, and interactive visualizations. Sisense enables the formation of Big Data repositories from multiple data sources and querying it through its dashboard, along with a full-stack analytics platform.
Many different color schemes can illustrate the heat map, with perceptual advantages and disadvantages for each. The color palette choices are more than just aesthetics because the colors in the HeatMap reveal patterns in the data. Good color schemes can enhance pattern discovery, and poor color choices can hide it.
Vary hue to distinguish categories: Vary the elements’ color to represent multiple plot categories. Most people can differentiate between a moderate number of shades. It is best to represent categories using hues.
Vary luminance to represent numbers: Varying brightness helps you see structure in numeric data. In Bivariate distribution, luminance variation enhances discrete or continuous pattern visualization. The luminance color scheme also makes apparent that there are two prominent peaks.
Color palette: Color is an essential component of heatmap; therefore, a color palette should match the data type. A sequential color bar is used between value and color, where lighter colors correspond to smaller amounts and darker shades to larger values, or vice versa. A diverging color palette is used when values have a zero point.
Legend: A legend is essential for viewers to understand the heatmap’s values, as colors have no inherent association with numeric values. A key is useful to show the mapping of colors to a numeric value.
Annotate: There is an absence of accuracy in mapping color to value; therefore, it is good to add cell value annotations to the heatmap.
Sort: It is recommended to plot the numeric variables in sorted order on the heatmap; it helps readers to grasp the data patterns. As categories do not have an inherent ordering, a standard principle is to sort them by their average cell value. In the clustered heatmap, category values are grouped by measurement of similarity.
Tick marks: Tick marks generally correspond to the number of bins, and the number of bins depends on the data’s nature. For numeric axis variables, If there are few bins, it is acceptable to keep tick marks on each bin. However, plotting tick marks between sets of bins is better to avoid overcrowding when there are many bins. For a categorical axis variable, to keep tick marks on each bin is recommended.
The seaborn Heatmaps are the grid Heatmaps that can take various types of data and generate heatmaps. The primary purpose of the seaborn heatmap is to show the correlation matrix by data visualization. It helps find the relationship between multiple features and which features are best for Machine Learning model building.
A correlation matrix denotes the correlation coefficients between variables at the same time. A heat map grid can represent these coefficients to build a visual representation of the variables’ dependence. This visualization makes it easy to spot the strong dependencies. A positive correlation indicates a strong dependency, while a negative correlation indicates a strong inverse dependency; a correlation coefficient closer to zero indicates weak dependence.
The heatmap transforms the correlation matrix into color codings. The correlation matrix shows how the variables are correlated to each other on a scale of -1 to 1. 1 being positively correlated and -1 being inversely correlated. As only numerical features can show the correlation, the heatmap cannot visualize categorical features. Strings need to be encoded to be pictured on the Heatmaps.
Three input types for the heatmap8
Wide-format: Wide-format, also called Untidy Format, is a matrix where each row is an individual, and each column represents an observation. In this case, a heatmap cell color corresponds to the observation value.
Correlation matrix: Correlation Matrix is also called square format formed by performing the corr() function on the dataset, and this matrix is plotted on the heatmap. Such Heatmaps helps discover which variables are related to each other.
Long format: Long format, also called tidy format, is when each line represents an observation. You have three columns: individual, variable name, and value (x, y, and z). You can plot a heatmap from this kind of data as follow:
seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None, fmt='.2g', annot_kws=None, linewidths=0, linecolor='white', cbar=True, cbar_kws=None, cbar_ax=None, square=False, xticklabels='auto', yticklabels='auto', mask=None, ax=None, **kwargs)
To understand each argument in the heatmap function, please refer to the following Document.7
Heatmap takes one mandatory parameter: ‘data.’ The CSV format dataset is downloaded from the below link.
Dataset: Red Wine quality
%matplotlib inlineimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as snsdata= pd.read_csv(“redwinequality.csv”)
sns.heatmap(data)
sns.heatmap(data.corr())
cmap parameter can take matplotlib colormap name or object, or list of colors.
The currently available Matplotlib colormap names are listed below:9
Perceptually Uniform Sequential ['viridis', 'plasma', 'inferno', 'magma']Sequential ['Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds', 'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu', 'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn']Sequential (2)['binary', 'gist_yarg', 'gist_gray', 'gray', 'bone', 'pink', 'spring', 'summer', 'autumn', 'winter', 'cool', 'Wistia', 'hot', 'afmhot', 'gist_heat', 'copper']Diverging['PiYG', 'PRGn', 'BrBG', 'PuOr', 'RdGy', 'RdBu', 'RdYlBu', 'RdYlGn', 'Spectral', 'coolwarm', 'bwr', 'seismic']Qualitative['Pastel1', 'Pastel2', 'Paired', 'Accent', 'Dark2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c']Miscellaneous['flag', 'prism', 'ocean', 'gist_earth', 'terrain', 'gist_stern', 'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv', 'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar']
Seaborn Color Palletes: The Color Palletes are a full range of digital colors used for visualizations using seaborn.
The seaborn function color_palette() provides an interface to generate color palettes in seaborn. The argument to color_palette() can be Name of a seaborn palette (deep, muted, bright, pastel, dark, colorblind), Name of matplotlib colormap, ‘ch:<cubehelix arguments>’, ‘husl’ or ‘hsl’, ‘light:<color>’, ‘dark:<color>’, ‘blend:<color>,<color>’, or A sequence of colors in any format matplotlib accepts
set_palette() function sets the default palette, which calls color_palette() internally, and accepts the same arguments.
Qualitative palette’s colors mostly have variations in their hue component; therefore, qualitative palettes are suitable to represent categorical data. The seaborn’s default color palette is a ‘qualitative palette.’ Seaborn has six matplotlib’s palette variations, called deep, muted, pastel, bright, dark, and colorblind.
The most straightforward approach to find unique hues is to draw evenly-spaced colors in a circular color space for an arbitrary number of categories; HSL & HUSL color space uses this approach.
A sequential palette is useful for representing numeric data as the primary dimension of variation in a sequential palette is luminance. Sequential color mapping is appropriate when data range from relatively low or uninteresting values to relatively great or exciting details. Seaborn uses the discrete version of the sequential palette for categorical data and the continuous version for numeric data. Discrete sequential colormaps can also be used for categorical data visualization. Sequential colormaps are perfect for data that experience a gradual and linear rise. Every sequential colormap has a reversed version, which has the suffix “_r.”
o Perceptually uniform palettes: Seaborn includes four perceptually uniform sequential colormaps: “rocket”, “mako”, “flare”, and “crest”. The first two have a vast luminance range and are well suited for applications such as heatmaps.
o Sequential cubehelix palettes: The cubehelix system offers an RGB-based tradeoff, which generates sequential palettes with a linear increase or decrease in brightness with some continuous variation in hue. The resulting colormaps are not perfectly perceptually uniform but have a parameterizable design process. cubehelix_palette() control how the palette looks.
o Custom sequential palettes: Both light_palette() or dark_palette() are passed a single color value, which produces a palette that starts either from light or dark desaturated values to that color.
o Sequential Color Brewer palettes: The Color Brewer library for sequential palettes include palettes with one primary hue.
Diverging palettes are suitable for representing numeric data with a categorical boundary. The diverging color palette creates a colormap as a combination of divergence between two colors. These are used for data where both low and high values are impressive and span a midpoint value that should be de-emphasized. Good diverging palettes contain two dominant hues in the colormap, one at each pole. It’s also essential that the starting values are of similar brightness and saturation.
o Perceptually uniform diverging palettes: Seaborn has two perceptually uniform diverging palettes: “vlag” and “icefire.”
o Custom diverging palettes: diverging_palette() is used to create a custom colormap for diverging data. It builds the colormaps with one color on each side. It needs two colors as arguments, converging to another color in the center. Correlations range from -1 to 1, so they have two directions, and in this case, a diverging palette works better than a sequential one.
Cubehelix palette is a sequential palate. Cmap can take matplot colormaps or seaborn palette as arguments. We can use both of the cubehelix colors in our Heatmap
sns.heatmap(data.corr(), cmap=’cubehelix’)
cmap= sns.cubehelix_palette()sns.heatmap(data.corr(), cmap=cmap)
seaborn.diverging_palette(h_neg=60, h_pos=100, s=50, l=50, sep=1, n=6, center='light', as_cmap=False)· h_neg and h_pos: For hues, float values range from 0–359, for negative and positive extents of the map.· S: saturation, float in [0, 100], optional· l: lightness, float value Ranges between 0–100.· sep: the size of the intermediate region, int· n: Number of colors in the palette, int, optional· center: {“light,” “dark”}, optional, state whether the center of the palette is light or dark· as_cmap: A boolean parameter, when set to True, represents a matplotlib colormap object.cmap = sns.diverging_palette(320, 40, sep=50, as_cmap=True)map = sns.heatmap(data.corr(),annot=True,cmap=cmap)
A colormap can be generated by a pre-built function with a blend of either light or dark values.
· Light palette colormap: It merges the given color from light to dark, representing the data from low to high values.
cmap = seaborn.light_palette("color-code",as_cmap=True)plt.figure(figsize=(15,12))cmap = sns.light_palette(“#3fdd01”, as_cmap=True)map = sns.heatmap(data.corr(),annot=True,cmap=cmap)
· Dark palette colormap: It mixes the given color from dark to light, representing the data from low to high values.
cmap = sns.dark_palette(“#3fdd01”, as_cmap=True)map = sns.heatmap(data.corr(),annot=True,cmap=cmap)
The seaborn.mpl_palette() function can visualize the discrete values by generating a discrete palette.
cmap = sns.mpl_palette(“Set3”, 20)map = sns.heatmap(data.corr(), annot=True, cmap=cmap)
· Set3: Name of the color palette
· 20: Number of discrete colors to be presented in a palette.
Parameters vmin & vmax are used to change the colormap's limits, which adjust the lower and upper contrast bounds. Everything below vmin and everything above vmax will be of the same colors, respectively.
vmin and vmax are set to lie outside of the range of the data for a washed-out look.
sns.heatmap(data.corr(), vmin=-0.2, vmax= 0.2)
cmap values are set to change the default colormap value.
sns.heatmap(data.corr(), cmap=”coolwarm”)
Data is centered on ‘0’ on a default colormap. The center can be changed by setting the ‘center’ parameter of the colormap to a specific value.
sns.heatmap(data.corr(), cmap=”coolwarm”, center=0.6)
Robust remove vmin or vmax using sns.heatmap() robust parameter. The ‘robust’ works like an “auto-contrast” for choosing good values by fixing contrast levels based on quartiles.
sns.heatmap(data.corr(), cmap=”coolwarm”, robust=True)
Annot parameter fills the cells with the relative correlation coefficient, which ranges from -1 to 1.
sns.heatmap(data.corr(), cmap=”coolwarm”, annot=True)
The ‘annot’ only adds numeric value on the python heatmap cell, but the ‘fmt’ parameter allows us to change these annotations' format and significantly add string (text) values. For string values, ‘annot’ should contain an array of strings, and ‘fmt’ should be ‘s.’
The default scientific notation is 2g. Fmt accepts the below values:
s    - stringf, F – fixed pointg, G - general formate, E - exponent format
Annotate with the numeric value:
sns.heatmap (data.corr(), cmap=’coolwarm’, annot=True, fmt=”.3f”)
data2=(data.corr()).round(3)corrA = np.squeeze(np.asarray(data2))print(corrA)rows, cols = (12, 12)arr_annot = [[(str(corrA[j][i])+’_’+str(j)+’_’+str(i)) for i in range(cols)] for j in range(rows)]sns.heatmap(data.corr(), cmap=’coolwarm’,annot = arr_annot, fmt=”s”)
Sometimes annot and fmt parameter is not sufficient to generate a meaningful and stylish Heatmap. For formatting of cell annotations, such as the font size, font color, font orientation, etc., can be done by annot_kws. The ‘annot_kws’ means annotate keyword arguments, which help change the style and formatting of annotating text.
The ‘annot_kws’ accepts all ‘ax.text’ or ‘matplotlib.text’ arguments. For passing annot_kws, then annot value must be True.7
· annot_kws: values as a dictionary of the key, value mappings, optional· fontsize: The size of the font· fontstyle: The style of font like italic, oblique and normal· fontfamily: a family of font like serif, cursive· color: the color of the font· alpha: transparency of the text· rotation: the rotation of the text· verticalalignment: vertical alignment (center, top, bottom, baseline, center_baseline)· backgroundcolor: Background color of the textsns.heatmap(data.corr(), cmap=”coolwarm”, annot=True, annot_kws={‘fontsize’:12, ‘fontstyle’:’italic’, ‘color’:”b”, ‘alpha’:0.6, ‘rotation’:”vertical”,’verticalalignment’:’center’, ‘backgroundcolor’:’w’})
Increase or decrease line width between each cell by the ‘linewidth’ parameter.
sns.heatmap(data.corr(), cmap=”coolwarm”, linewidth=0.5)
Sometimes the ‘linewidth’ parameter fails to divide the heatmap cell because of color complexity. The ‘linecolor’ parameter gives the flexibility to choose any color for the heatmap line. The ‘linewidth’ parameter is mandatory to set the ‘linecolor.’ For the list of colors, refer to the following document.
sns.heatmap(data.corr(), cmap=”coolwarm”, linewidth=0.5, linecolor=’g’)
The legend or ‘cbar’ is visible by default. It doesn’t make much sense of heatmap without the key. Still, if the key is not needed, ‘cbar’ is set to ‘False.’
sns.heatmap(data.corr(), cmap=”coolwarm”, cbar=False)
The color bar keyword arguments (cbar_kws) parameter can accept multiple keyword arguments as a dictionary. The ‘cbar_kws’ accepts all matplotlib color bar parameters.7
· orientation: the direction of the color bar, ‘horizontal’ or ‘vertical.’· shrink: the size of the color bar· extend: extend used to taper the color bar. Values are ‘both, ‘min, ‘max,’ and ‘neither’ depending upon the requirement.· extendfrac: ‘extendfrac’ is used to adjust the extension of the color bar. ‘False’ value for no pointer, ‘auto’ value adjust pointer automatically, and float value to adjust the color bar pointer manually.· ticks: The ‘ticks’ parameter is used to change the color bar’s ticks; the value can be a list or numpy array of ticks· drawedges: For edges on the color bar. Value is Boolean. ‘True’ for edges, ‘False’ for no edges.sns.heatmap(data.corr(), cmap=”coolwarm”, cbar_kws={“label”:”Wine Quality Colorbar”, “orientation”:”horizontal”, “shrink”:1, ‘extend’:’both’, ‘extendfrac’:0.1, “ticks”:np.arange(0,3), “drawedges”:True })
ax and cbar_ax are used to plot heatmap and cbar on separate axes.
f, (ax, cbar_ax) = plt.subplots(1,2, figsize=(15,12), gridspec_kw={“wspace”:0.05, “width_ratios”:(0.8,0.1)} )ax = sns.heatmap(data.corr(), cmap=’coolwarm’, ax=ax,   cbar_ax=cbar_ax)
The ‘ax’ is also used to set various axes level functions such as title, x label, y label, etc.
f, (ax, cbar_ax) = plt.subplots(1,2, figsize=(15,12), gridspec_kw={“wspace”:0.05, “width_ratios”:(0.8,0.1)} )sns.heatmap(data.corr(), cmap=’coolwarm’, ax=ax,cbar_ax=cbar_ax)ax.set(title=”Wine Quality Heatmap”,xlabel=”Feature Rows”,ylabel=”Feature Columns”)
‘square’ parameter is used to shape the Heatmap cells as squares. The ‘square’ forces the aspect ratio of the blocks to be equal.
sns.heatmap(data.corr(), cmap=”coolwarm”, square=True)
To set the labels on X-axis and Y-axis, ‘xticklabel’ and ‘yticklabel’ parameters are used.7
xticklabel: To place tags on the x-axis of heatmap. ‘xticklabels’ is an optional parameter that accepts arguments as “auto,” bool, list-like, numpy array, or int.False: to hide the labelsArray/list/int: to set values, other than defaultsns.heatmap(data.corr(),cmap=”coolwarm”,xticklabels = np.arange(0,12))
Rotation of label ticks is possible by using the ‘rotation’ parameter.
To rotate the default labels, ‘get_yticklabels()’ or ‘get_xticklabels()’ is used to get the default labels, then rotation is set to an angle value.
HM=sns.heatmap(data.corr(), cmap=”coolwarm”)HM.set_yticklabels(HM.get_yticklabels(), rotation=40)
The entire heatmap can be divided into two triangles, the upper triangle and the lower triangle. Both the triangle provides a complete set of information alone. Therefore it is redundant to plot the entire heatmap. It is preferred to plot either of the triangles.
To mask the upper triangle, ‘triu,’ a numPy method is used. To hide the lower triangle, ‘tril,’ a numPy method is used.
sns.heatmap(data.corr(),cmap=”coolwarm”,mask= np.triu(data.corr()))
To hide specific values, the condition to hide the desired values is passed in the ‘mask’ parameter. Here, we are masking every alternate row starting from row 1 and every alternate column starting from column 0.
mask = np.zeros(data.corr().shape)mask[1::2,0::2] = 1sns.heatmap(data.corr(), annot=True,cmap=”coolwarm”,mask= mask )
data1=(data.corr()).round(3)corrA = np.squeeze(np.asarray(data1))mask = np.zeros(data.corr().shape)for i in range(12):for j in range(12):if corrA[i][j]<0:mask[i][j]=1plt.figure(figsize=(15,12))sns.heatmap(data.corr(), annot=True,cmap=”coolwarm”,mask= mask )
The ‘kwargs’ is used to pass other keyword arguments, generally not available in seaborn such as rasterized, edge color, cap-style, etc. It allows all keyword arguments of matplotlib ‘ax.pcolormesh.’
Kwargs accepts a dictionary as an argument.
kwargs = {‘alpha’:.9,’linewidth’:3, ‘linestyle’:’ — ‘, ‘linecolor’:’k’,’rasterized’:False, ‘edgecolor’:’w’, “capstyle”:’projecting’,}sns.heatmap(data.corr(), cmap=”coolwarm”, **kwargs )
seaborn.set_theme(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1, color_codes=True, rc=None)12
The set_style() interface can be used for manipulating the styles. It helps to set the theme of the plot. There are five themes available:
Darkgrid
Whitegrid
Dark
White
Ticks
The default theme is darkgrid.
The top and right axis spines can be removed using the despine() function in the white and ticks themes.
The Heatmap scale can be controlled by using the set_context() function or set_theme() function. Based on relative size, there are four pre-set templates for contexts:
Paper
Notebook
Talk
Poster
By default, the context is the notebook.
The font size of all the seaborn components can be changed using Seaborn's ‘font_scale’ attribute. The default scale is ‘1’.
sns.set_theme(context=’paper’, style=’ticks’,font_scale=2)sns.heatmap(data.corr())sns.despine()
Multiple Heatmaps can be generated using the matplotlib plt.subplot() function.
plt.subplot(1,3,1)sns.heatmap(data.corr(), cbar=False, linecolor=”w”, linewidths=1)plt.subplot(1,3,2)sns.heatmap(data=data[[‘quality’,’alcohol’]].corr(), cmap=’inferno’)plt.subplot(1,3,3)sns.heatmap(data.corr(), cmap=”coolwarm”,mask= np.tril(data.corr()), cbar=False )
sns.set_theme(context=’paper’, style=’ticks’,font_scale=2)f, (ax, cbar_ax) = plt.subplots(1,2, figsize=(15,12), gridspec_kw={“wspace”:0.05, “width_ratios”:(0.8,0.05)} )mask = np.triu(data.corr())annot_kws={‘fontsize’:14, ‘fontstyle’:’italic’,’fontweight’:’bold’, ‘color’:”k”, ‘alpha’:0.6, ‘rotation’:None,’verticalalignment’:’center’, ‘backgroundcolor’:’None’}cbar_kws={“label”:”Wine Quality Colorbar”,”orientation”:”vertical”, “shrink”:1,’extend’:’max’, ‘ticks’:[-0.8, -0.5,0, 0.5], ‘extendfrac’:0.1, “drawedges”:True }sns.heatmap(data.corr(), vmin=None, vmax=0.7, cmap=’BuGn’, center=None, robust=True, annot=True, fmt=’.2F’, annot_kws=annot_kws, linewidths=0.8, linecolor=’white’, cbar=True, cbar_kws=cbar_kws, cbar_ax=cbar_ax, square=True, xticklabels=’auto’, yticklabels=’auto’, mask=mask, ax=ax)f.suptitle(“Wine Quality Heatmap”, fontsize=24, fontweight=’bold’)ax.set(xlabel=”Feature Rows”, ylabel=”Feature Columns”)
Wine Quality has the highest positive correlation with alcohol, followed by sulfates & citric acid.
Wine quality has the highest negative correlation with volatile acidity, followed by density and chlorides.
If dimensionality reduction is needed to develop a model, we can eliminate the features correlating closer to zero, such as residual sugar, sulfur dioxide, pH, and fixed acidity.
One of the features with a high correlation with other components can also be omitted for feature selection. One of the ‘free sulfur dioxide’ or ‘total sulfur dioxide’ features can be omitted (depending upon further analysis).
Regular Heatmaps makes it easy to distinguish between negative correlation, positive correlation, and no correlation features, but to examine the values of positive correlation among positively correlated variables still require us to go over the grid multiple times.
To make our maps more readable, in addition to color, we add size as a parameter to our heatmap. The size of each colored square is proportional to the magnitude of the correlation.
It also gives intuition about the marginal distributions without the need to refer to a color legend. There is no feature in seaborn for this; we will use corrplot from biokit.
c = corrplot.Corrplot(data.corr())c.plot(cmap=’coolwarm’, method=’square’, shrink=.9 ,rotation=45)
This type of heatmap implements clustering as part of the process of grouping similar features. Inherently Clustering algorithms are used to arrange similar rows together on the map. Similarly, the order of the columns is determined.
sns.clustermap(data.corr(), cmap="coolwarm")
Thank you for reading this article. I believe I have covered all the possible content related to Heatmaps. Let me know if I have missed any in the comments. You can also explore the colab notebook containing the above examples and more. Till then, Happy Learning. Tia