Python: How to plot heat map of 2D matrix by ignoring zeros? -


i have matrix of size 500 x 28000, contains lot of zeros in between. let consider working example matrix a:

a = [[0, 0, 0, 1, 0],     [1, 0, 0, 2, 3],     [5, 3, 0, 0, 0],     [5, 0, 1, 0, 3],     [6, 0, 0, 9, 0]] 

i plot heatmap of above matrix, since contains lot of zeros, heatmap contains white space seen in figure below.

how can ignore zeros in matrix , plot heatmap?

here minimal working example tried:

im = plt.matshow(a, cmap=pl.cm.hot, norm=lognorm(vmin=0.01, vmax=64), aspect='auto') # pl pylab imported pl plt.colorbar(im) plt.show() 

which produces:

enter image description here

as can see because of zeros white spaces appear.

but original matrix of size 500x280000 contains lot of zeros, makes colormap white!!

this answer in same direction 'edit 2' section of luis' answer. in fact, simplified version of it. posting in order correct misleading statements in comments. saw warning should not discuss in comment area, using answering area.

anyway, first let me post code. please note used larger matrix randomly generated inside script, instead of sample matrix a.

#!/usr/bin/python # # script written norio 2016-8-5.  import os, re, sys, random import numpy np  #from matplotlib.patches import ellipse import matplotlib mpl import matplotlib.pyplot plt import matplotlib.image img  mpl.rcparams['lines.linewidth'] = 2 mpl.rcparams['lines.markeredgewidth'] = 1.0 mpl.rcparams['axes.formatter.limits'] = (-4,4) #mpl.rcparams['axes.formatter.limits'] = (-2,2) mpl.rcparams['axes.labelsize'] = 'large' mpl.rcparams['xtick.labelsize'] = 'large' mpl.rcparams['ytick.labelsize'] = 'large' mpl.rcparams['xtick.direction'] = 'out' mpl.rcparams['ytick.direction'] = 'out'   ############################################ #numrow=500 #numcol=280000 numrow=50 numcol=28000 # .. testing numelm=numrow*numcol eps=1.0e-9 # #numnz=int(1.0e-7*numelm) numnz=int(1.0e-5*numelm) # .. testing vmin=1.0e-6 vmax=1.0 outfigname='stackoverflow38790536.png' ############################################  ### data matrix # generating data matrix here artificially. print 'generating pseudo-data..' random.seed('20160805') mata=np.zeros((numrow, numcol)) je in range(numnz):     jr = random.uniform(0,numrow)     jc = random.uniform(0,numcol)     mata[jr,jc] = random.uniform(vmin,vmax)   ### actual processing given data start here print 'processing..'  idxrow=[] idxcol=[] val=[] ii in range(numrow):     jj in range(numcol):         if np.abs(mata[ii,jj])>eps:             idxrow.append(ii)             idxcol.append(jj)             val.append( np.abs(mata[ii,jj]) )  print 'len(idxrow)=', len(idxrow)     print 'len(idxcol)=', len(idxcol)     print 'len(val)=',    len(val)       ############################################ # canvas setting line plots  ############################################  f_size   = (8,5)  a1_left   = 0.15 a1_bottom  = 0.15 a1_width  = 0.65 a1_height = 0.80 # hspace=0.02 # ac_left   = a1_left+a1_width+hspace ac_bottom = a1_bottom ac_width  = 0.03 ac_height = a1_height  ############################################ # plot  ############################################ print 'plotting..'  fig1=plt.figure(figsize=f_size) ax1 =plt.axes([a1_left, a1_bottom, a1_width, a1_height], axisbg='w')  pc1=plt.scatter(idxcol, idxrow, s=20, c=val, cmap=mpl.cm.gist_heat_r) # cf. # http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter plt.xlabel('column index', fontsize=18) plt.ylabel('row index', fontsize=18) ax1.set_xlim([0, numcol-1]) ax1.set_ylim([0, numrow-1])  axc =plt.axes([ac_left, ac_bottom, ac_width, ac_height], axisbg='w') mpl.colorbar.colorbar(axc,pc1, ticks=np.arange(0.0, 1.5, 0.1) )  plt.savefig(outfigname) plt.close() 

this script output figure, 'stackoverflow38790536.png', following. scatter plot of non-zero elements

as can see in code, used scatter instead of plot. realized plot command not best suitable task here.

another of words need correct row_index not need have as 140,000,000(=500*280000) elements. need have row indices of non-zero elements. more correctly, lists, idxrow, idxcol, , val, enter scatter command in code above, has lengths equal number of non-zero elements.

please note both of these points have been correctly taken care of in luis' answer.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -