如何为大型阵列渲染热图

How to render a heatmap for a large array

我有一个大型数据集,我从中导出一个平方矩阵,我想将其可视化为热图。我正在使用 Matplotlib 和 Seaborn。不幸的是,它似乎只适用于相对少量的数据。

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

大约从 size=6000 开始,这将停止工作,导致出现白色热图。

imshowmatshow 似乎工作正常:

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

输出:

  • 原代码没有为我生成情节
  • fig, ax = plt.subplots() 更改为 plt.figure(figsize=(14, 14)),有助于创建情节。
    • figsize=(10, 10),该图未在 Jupyter 中呈现,但正确的图像已保存到文件中。
    • 小于 figsize=(14, 14) 的图形不会在 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()