Heatmap is a representation of data in 2D form using different colors in the form of graph. The darker shades is used to indicate higher values  than the lighter shade. It is mainly known for calculating data intensity and to find correlation between different data sets. Heatmaps can be used in geographical area to calculate density of certain instances, travel industry to  keep the track of flights & passengers, in stock market to indicate which stock options are bullish and which ones are bearish etc.

Heatmaps in python can be implemented using different library functions like matplotlib, seaborn, plotly, cufflinks, bokeh etc. Below are few examples how heatmaps are represented.

Let’s discuss about simple heatmap using Matplotlib library function. Firstly, import matplotlib library function.

import numpy as np

import matplotlib.pyplot as plt Matplotlib can be build using imshow function. We can use random data or a specific dataset. After this imshow function is called where we pass the data, colormap value and interpolation method.

data = np.random.random((12, 12))
plt.imshow(data, cmap = 'autumn', interpolation = 'nearest')
plt.title("2-D Heat Map")

Now let’s go through categorial heatmap where we can set xlabel and ylabel which are known as tick labels.

Lets import the required modules :-          

import numpy as np
import matpotlib
import matplotlib.pyplot as plt
vegetables = ["cucumber", "tomato", "lettuce", "asparagus",
              "potato", "wheat", "barley"]
farmers = ["Farmer Joe", "Upland Bros.", "Smith Gardening",
           "Agrifun", "Organiculture", "BioGoods Ltd.", "Cornylee Corp."]
harvest=np.array([[0.8, 2.4, 2.5, 3.9, 0.0, 4.0, 0.0],
                    [2.4, 0.0, 4.0, 1.0, 2.7, 0.0, 0.0],
                    [1.1, 2.4, 0.8, 4..3, 1.9, 4.4, 0.0],
                    [0.6, 0.0, 0.3, 0.0, 3.1, 0.0, 0.0],
                    [0.7, 1.7, 0.6, 2.6, 2.2, 6.2, 0.0],
                    [1.3, 1.2, 0.0, 0.0, 0.0, 3.2, 5.1],
                    [0.1, 2.0, 0.0, 1.4, 0.0, 1.9, 6.3]])
fig, ax = plt.subplots()
im = ax.imshow(harvest)
# We want to show all ticks...
#and label them with the respective list entries
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
# Loop over data dimensions and create text annotations.
for i in range(len(vegetables)):
    for j in range(len(farmers)):
        text = ax.text(j, i, harvest[i, j],
                       ha="center", va="center", color="w")
ax.set_title("Harvest of local farmers (in tons/year)")




Majorily we discuss about imshow function here and the strength of heatmaps where we use color to get information across .

Leave a Comment

Your email address will not be published. Required fields are marked *