Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : Plot heatmap for large matrix

I have a large matrix of size 500 X 18904.

Since most of the values are zeros, I'm not able to visualise the pattern clearly as the zeroes dominate in the colorbar.

To look at the data more closely, I need to zoom in for different segments of image. Is there any reliable way to visualise this data using colorbar?.

Here is my code and output.

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

The output enter image description here

like image 240
Rangooski Avatar asked Feb 07 '23 10:02

Rangooski


1 Answers

I can think of two options by using numpy arrays.

  1. Assuming your data is mostly higher than zero but there are a lot of zeros.:

    vmin = some_value_higher_than_zero
    plt.matshow(k,aspect='auto',vmin=vmin)
    
  2. Setting all zeros to NaNs. they are automatically left out.

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

NB. imshow and matshow work both here.

Another option, when your matrix is really sparse is to use scatterplots.

x,y = k.nonzero()
plt.scatter(x,y,s=100,c=k[x,y]) #color as the values in k matrix
like image 134
JLT Avatar answered Feb 13 '23 02:02

JLT