python - Make reverse diagonals white in heatmap -
i'm trying seen on image given below, 
just setting reverse diagonals white color left. couldn't set them white. chart takes integer values , don't know integer value corresponding of white color.
thank!
edited:
here code;
import math matplotlib import pyplot plt matplotlib import cm cm import pylab import numpy np matplotlib.collections import linecollection class heatmap: def __init__(self, selectedlines): self.selectedlines = selectedlines def getheapmap(self): figure = plt.figure() if len(self.selectedlines) != 0: self.map = self.createtestmapdata(len(self.selectedlines), len(self.selectedlines)) maxvalueinmap = self.findmaxvalueinmap(self.map) x = np.arange(maxvalueinmap + 1) ys = [x + in x] ax = figure.add_subplot(111) ax.imshow(self.map, cmap=cm.jet, interpolation='nearest') ''' left side label of chart created according selected values checkbox group. ''' leftsidelabelsize = len(self.selectedlines) sidelabels = [] line in self.selectedlines: sidelabels.append(line.text()) pos = np.arange(leftsidelabelsize) ''' left side labels set code below. ''' pylab.yticks(pos, sidelabels) plt.xticks(pos, sidelabels) self.numrows, self.numcols = self.map.shape ax.format_coord = self.format_coord line_segments = linecollection([zip(x, y) y in ys], linewidths=(0.5, 3, 1.5, 2), linestyles='solid') line_segments.set_array(x) axcb = figure.colorbar(line_segments) return figure def format_coord(self, x, y): col = int(x + 0.5) row = int(y + 0.5) if col >= 0 , col < self.numcols , row >= 0 , row < self.numrows: z = self.map[row, col] return 'x=%1.4f, y=%1.4f, z=%1.4f' % (x, y, z) else: return 'x=%1.4f, y=%1.4f' % (x, y) def createtestmapdata(self, xsize, ysize): resultmap = 10 * np.random.rand(xsize, ysize) #setting reverse diagonal here. set 0 gives blue. # want set white index in range(0, int(math.sqrt(resultmap.size))): resultmap[index][((math.sqrt(resultmap.size) - 1) - index )] = 0 return resultmap def findmaxvalueinmap(self, map): return np.amax(map) the values generated randomly @ moment. code above gives gui like;

you can make own colormap, or adjust existing 1 :)

here's code above plot, explainations in comments:
import matplotlib pylab import * import numpy np #create test data 0 valued diagonal: data = np.random.random_sample((25, 25)) rows, cols = np.indices((25,25)) data[np.diag(rows, k=0), np.diag(cols, k=0)] = 0 #create new colormap, white 0 #(can take rgb values, (255,255,255): colors = [('white')] + [(cm.jet(i)) in xrange(1,256)] new_map = matplotlib.colors.linearsegmentedcolormap.from_list('new_map', colors, n=256) pcolor(data, cmap=new_map) colorbar() savefig('map.png') show() alternatively, mask data, , set mask color:
#create test data: data = np.random.random_sample((25, 25)) #create diagonal mask: mask = np.diag(np.ones(25)) #apply mask data: masked_data = ma.masked_array(data, mask) #set mask color white: cm.jet.set_bad(color='white', alpha=none) #for work use pcolormesh instead of pcolor: pcolormesh(masked_data, cmap=cm.jet) colorbar() show() this produces same result, may suit needs better can set cell white, , white doesn't show on colorbar (see bottom of above colorbar):

Comments
Post a Comment