python : Plot heatmap for large matrix

标签 python python-3.x matplotlib heatmap colorbar

我有一个大小为 500 X 18904 的大矩阵。

由于大多数值都是零,因此我无法清楚地看到图案,因为零在颜色条中占主导地位。

为了更仔细地查看数据,我需要放大图像的不同部分。有什么可靠的方法可以使用颜色条来可视化这些数据吗?

这是我的代码和输出。

import numpy as np
import matplotlib.pyplot as plt
import scipy.io as sio
j = sio.loadmat('UV_matrix.mat')
k = j['UV']  
plt.imshow(k, aspect='auto')
plt.show()

输出 enter image description here

最佳答案

我可以通过使用 numpy 数组想到两个选项。

  1. 假设您的数据大部分高于零,但也有很多零。

    vmin = some_value_higher_than_zero
    plt.matshow(k,aspect='auto',vmin=vmin)
    
  2. 将所有零设置为 NaN。它们会自动被排除在外。

    k[k==0.0]=np.nan
    plt.matshow(k,aspect='auto')
    

注意。 imshow 和 matshow 都在这里工作。

当您的矩阵非常稀疏时,另一种选择是使用散点图。

x,y = k.nonzero()
plt.scatter(x,y,s=100,c=k[x,y]) #color as the values in k matrix

关于 python : Plot heatmap for large matrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38726087/

相关文章:

python - 将 pandas hub_table 与 Interval 列一起使用会导致 TypeError

python - django迁移中如何获取当前数据库?

python - 在python中填充数组

python - 在 python telethon 中加入 channel 的最简单方法

python - 如何在虚拟环境中使用 pip

python - 使用 pygame 扭曲图像

linux - 如何获得蓝牙可配对和可发现状态

numpy - 使用来自 matplotlib 的 plt.plot() 将所有 numpy 数组绘图点相互连接

python - 在 Seaborn 图中指定独立轴值

matplotlib - pngfix.c :2151: undefined reference to `inflateReset2'