使热图中的对角线变为白色 [英] Make reverse diagonals white in heatmap

查看:57
本文介绍了使热图中的对角线变为白色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些如下图所示的事情,

I'm trying to do something as seen on the image is given below,

只剩下设置反向对角线白色.我不能将它们设置为白色.图表采用整数值,我不知道白色对应什么整数值.

Just setting reverse diagonals white color is left. I couldn't set them as white. The chart takes integer values and I don't know what integer value is corresponding of white color.

谢谢!

这是代码;

import math
from matplotlib import pyplot as plt
from matplotlib import cm as cm
import pylab
import numpy as np
from 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 + i for i in x]
            ax = figure.add_subplot(111)
            ax.imshow(self.map, cmap=cm.jet, interpolation='nearest')

            '''
            Left side label of the chart is created according to selected values
            from a checkbox group.
            '''
            leftSideLabelSize = len(self.selectedLines)
            sideLabels = []
            for line in self.selectedLines:
                sideLabels.append(line.text())
            pos = np.arange(leftSideLabelSize)
            '''
            Left side labels are set with the 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) for 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 and col < self.numcols and row >= 0 and 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 is here. Now it is set with zero but it gives blue.
        # I want it to be set as white
        for 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 are generated randomly at the moment. The code is above gives a gui like;

推荐答案

您可以制作自己的颜色图,或调整现有的颜色图 :)

You can make your own colormap, or adjust an existing one :)

下面是上图的代码,注释中有解释:

Here's the code for the above plot, with explainations in the comments:

import matplotlib
from pylab import *
import numpy as np

#Create test data with zero 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, with white for zero 
#(can also take RGB values, like (255,255,255):
colors = [('white')] + [(cm.jet(i)) for 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, you could mask your data, and set a mask color:

#Create test data:
data = np.random.random_sample((25, 25))
#Create a diagonal mask:
mask = np.diag(np.ones(25))
#Apply mask to data:
masked_data = ma.masked_array(data, mask)
#Set mask color to white:
cm.jet.set_bad(color='white', alpha=None)
#for this to work we use pcolormesh instead of pcolor:
pcolormesh(masked_data, cmap=cm.jet)
colorbar()
show()

这会产生基本相同的结果,但是可以更好地满足您的需求,因为您可以将任何单元格设置为白色,并且白色不会出现在颜色栏上(请参见颜色栏的最底部):

This produces essentially the same result, but may suit your needs better as you can set any cell to white, and also the white doesn't show up on the colorbar (see very bottom of above colorbar):

这篇关于使热图中的对角线变为白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆