How to render a heatmap for a large array

Viewed 1120

I have a large dataset from which I derive a squared matrix I would like to visualize as a heatmap. I'm using Matplotlib and Seaborn. Unfortunately, it seems to work only for a relatively small amount of data.

        size = 10000
        similarity_matrix = np.random.rand(size, size)

        fig, ax = plt.subplots()
        sns.heatmap(similarity_matrix, vmin=0, vmax=1)

        plt.savefig("matrix.png")

This stops working from approximately size=6000, resulting in a white heatmap.

Heatmap

2 Answers

imshow or matshow seems to work fine:

np.random.seed(42)
size = 10000
similarity_matrix = np.random.rand(size, size)
plt.imshow(similarity_matrix, cmap='hot')
plt.colorbar()

Output:

enter image description here

  • The original code didn't generate a plot for me
  • Changing fig, ax = plt.subplots() to plt.figure(figsize=(14, 14)), worked to create the plot.
    • At figsize=(10, 10), the figure didn't render in Jupyter, but the correct image did save to a file.
    • A figure smaller than figsize=(14, 14), wouldn't render in Jupyter.
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# create matrix
size = 10000
similarity_matrix = np.random.rand(size, size)

# plot matrix

# create figure and set size
plt.figure(figsize=(14, 14))

# add heatmap
sns.heatmap(similarity_matrix, vmin=0, vmax=1)

# save the figure
plt.savefig('test.png', dpi=600)

# show the figure; this was slow
plt.show()

enter image description here

Related