How to visualize (plot) a numpy array in python using seaborn ?

Published: April 12, 2021

Tags: Python; Numpy; Array; Seaborn;

DMCA.com Protection Status

Examples of how to visualize (plot) a numpy array in python using seaborn

Create an a numpy array

Let's first create a random numpy array:

import numpy as np

data = np.random.randint(10, size=(10,8))

print(data)

returns for example

[[9 6 7 8 6 4 4 9]
 [1 1 4 0 4 6 0 1]
 [6 9 2 2 8 6 8 0]
 [9 8 9 1 4 2 2 3]
 [3 3 4 8 9 9 5 4]
 [5 4 2 8 7 3 4 7]
 [0 1 0 0 0 3 0 2]
 [7 2 6 5 4 4 5 2]
 [5 2 6 5 6 2 2 2]
 [3 1 0 5 9 2 2 2]]

Plotting an array with seaborn

Note: If you want to quickly visualize a not too large numpy array, a solution is to use seaborn with heatmap, example

import seaborn as sns; sns.set()
import matplotlib.pyplot as plt

ax = sns.heatmap(data, annot=True, fmt="d")

plt.title("How to visualize (plot) \n a numpy array in python using seaborn ?",fontsize=12)

plt.savefig("visualize_numpy_array_01.png", bbox_inches='tight', dpi=100)

plt.show()

returns

How to visualize (plot) a numpy array in python using seaborn ?
How to visualize (plot) a numpy array in python using seaborn ?

Removing the colorbar

ax = sns.heatmap(data, annot=True, fmt="d", cbar=None)

plt.title("How to visualize (plot) \n a numpy array in python using seaborn ?",fontsize=12)

plt.savefig("visualize_numpy_array_02.png", bbox_inches='tight', dpi=100, )

plt.show()

How to visualize (plot) a numpy array in python using seaborn ?
How to visualize (plot) a numpy array in python using seaborn ?

Removing axis labels

ax = sns.heatmap(data, annot=True, fmt="d", cbar=None, xticklabels=False, yticklabels=False)

plt.title("How to visualize (plot) \n a numpy array in python using seaborn ?",fontsize=12)

plt.savefig("visualize_numpy_array_03.png", bbox_inches='tight', dpi=100, )

plt.show()

How to visualize (plot) a numpy array in python using seaborn ?
How to visualize (plot) a numpy array in python using seaborn ?

References

Image

of