Heatmap Plotting in Python – The Simplest Way


Data visualization

This short tutorial teaches how to use Python seaborn which relies on matplotlib to plot a heatmap the simplest way.

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):
    plt.figure(1, figsize=(9, 6))


    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)

# 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")

More Resources