This short tutorial teaches how to use Python seaborn, which relies on matplotlib to plot a heatmap the most straightforward way.
1. What is A Heat Map?
Its origin dates back to the 19th century when manual grayscale shading was used to illustrate different raster data. At that time, a fintech entrepreneur named Cormac Kinney designed hardware for the real-time graphical display of financial market information. . Currently, many are still learning how to create this tool manually, but this time using specialized software, the best known of which is Excel.
This invention applies to website owners as it classifies items from the most famous to the least popular. This is done using a variety of colors, generally ranging from red to blue. Thus, users can determine the interest that the general public has in a particular web page. This insight is an incredible decision-making tool. It displays trends, which usually forces the owner to optimize their online platform.
1.1. What can you see on a heat map?
To show the current trend, the heatmap highlights a spectrum of warm and cool colors. The visitor can thus see if a page is more interesting than another. This shows the strolling distance of the page. If all goes well, the CTA above the fold turns a bright orange color.
In short, we can consider this tool as “a form of visual storytelling.” Indeed, at a glance, it shows the behavior and attitude of visitors to a given page. It also helps address an important issue, that of the appropriate location to consider for its content.
1.2. How does Heat Map work?
The principle is simple. The software collects information inside a web page. These, in turn, will appear on that same page. Thus, following the choice of a web page, it will be necessary to select its URL. While the HTML is loading, the data is transmitted, in parallel, to servers. This is made possible by “a short section of JavaScript” integrated into the code of the site itself. Each element of the page will then be translated into a map.
Each item here relates to anything likely to elicit interaction with visitors. To this will be added the various tags, without forgetting the parent elements. It is only after that the actual collection of activity data will take place.
A signal is then emitted automatically each time an Internet user acts on a said web page. It should be noted that this show is the result of a succession of behind-the-scenes interactions.
Now, if a visitor clicks on a web page’s CTA, that click is saved. It will then be added to all the data previously recorded. Thus obtaining a summary of what each individual did on the page by looking at the level of the dashboard.
The heat map exposes the most clicked places, creating a bright area known as “hot spots.” Even a beginner will be able to determine which places the site the most consulted. It is also possible to define which ones require more commitment or even more animation.
2. Heatmap in Python with Seaborn and Matplotlib
First and foremost, please make sure you have seaborn installed.
Here, we generate a 20 x 30 matrix with random values and plot the data using the function “plot_heatmap”; please see code below:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import seaborn
import matplotlib.pyplot as plt
def plot_heatmap(data, xlabels, ylabels, output_filename):
seaborn.set(color_codes=True)
plt.figure(1, figsize=(9, 6))
plt.title("Heatmap")
seaborn.set(font_scale=1.4)
ax = seaborn.heatmap(data, cmap="YlGnBu", cbar_kws={'label': 'Scale'})
ax.set_xticklabels(xlabels, rotation=45)
ax.set_yticklabels(ylabels, rotation=45)
ax.set(ylabel="X-Label", xlabel="Y-Label")
plt.savefig(output_filename, bbox_inches='tight', dpi=300)
plt.close()
# define data
data = np.random.rand(20, 30)
# define labels
x_labels = ["x_{}".format(x) for x in range(1, 31)]
y_labels = ["y_{}".format(x) for x in range(1, 21)]
# create heat map
plot_heatmap(data, x_labels, y_labels, "heatmap.png")
2. More Resources
- Confusion Matrix Plotting – The Simplest Way
- The Easiest way to Plot a Histogram in Python – Step-by-Step
- Simple Box Plot and Swarm Plot in Python