热图上的特定离群值-matplotlib [英] specific outliers on a heat map- matplotlib

查看:176
本文介绍了热图上的特定离群值-matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个具有固定的异常值的数据生成一个热图,我需要将这些异常值显示为我使用的cmap调色板中的一种颜色,该颜色是热"的.通过使用cmap.set_bad('green')和np.ma.masked_values(data,outlier),我得到了一个看起来正确的图,但是即使使用cmap.set_over,颜色条也无法与数据正确同步. ('绿色'). 这是我一直在尝试的代码:

I am generating a heat map with data that has a fixed outlier number and I need to show these outliers as a colour out of the colour palette of the cmap I use which is "hot". With the use of cmap.set_bad('green') and np.ma.masked_values(data, outlier), I get a plot which looks right but the color bar is not getting synced with the data properly even if I use cmap.set_over('green'). Here is the code I have been trying:

plt.xlim(0,35)
plt.ylim(0,35)
img=plt.imshow(data, interpolation='none',norm=norm, cmap=cmap,vmax=outlier)

cb_ax=fig.add_axes([0.85, 0.1, 0.03, 0.8])

cb=mpl.colorbar.ColorbarBase(cb_ax,cmap=cmap,norm=norm,extend='both',spacing='uniform')
cmap.set_over('green')
cmap.set_under('green')

以下是数据(显然离群值是1.69):

Here is the data (outlier is 1.69 obviously):

Data;A;B;C;D;E;F;G;H;I;J;K    
A;1.2;0;0;0;0;1.69;0;0;1.69;1.69;0    
B;0;0;0;0;0;1.69;0;0;1.69;1.69;0    
C;0;0;0;0;0;1.69;0;0.45;1.69;1.69;0.92    
D;1;0;-0.7;-1.2;0;1.69;0;0;1.69;1.69;0    
E;0;0;0;0;0;1.69;0;0;1.69;1.69;0    
F;1.69;1.69;1.69;1.69;1.69;1.69;1.69;1.69;1.69;1.69;1.69    
G;0;0;0;0;0;1.69;0;0;1.69;1.69;0    
H;0;0;0;0;0;1.69;0;0;1.69;1.69;0    
I;1.69;1.69;1.69;1.69;1.69;1.69;1.69;1.69;1.69;1.69;1.69
J;1.69;1.69;1.69;1.69;1.69;1.69;1.69;1.69;1.69;1.69;1.69
K;0;0;0;0;0;1.69;0;0;1.69;1.69;0

感谢任何帮助

推荐答案

正在发生的事情是,您使用的是遮罩了异常值的遮罩数组.

What's happening is that you're using a masked array where the outliers are masked.

因此,它们不会在颜色栏上显示为上方". (即就matplotlib而言,被屏蔽的值是无效的,未超过阈值)

Therefore, they don't show up on the colorbar as being "over". (i.e. as far as matplotlib is concerned, the masked values are invalid, not over the threshold)

作为重现您的问题的独立示例:

As a stand-alone example to reproduce your problem:

import numpy as np
import matplotlib.pyplot as plt

threshold = 0.8
data = np.random.random((10,10))
data = np.ma.masked_greater(data, threshold)

fig, ax = plt.subplots()
im = ax.imshow(data, cmap=plt.cm.hot, interpolation='none')
cbar = fig.colorbar(im, extend='max')
cbar.cmap.set_over('green')

plt.show()

如果我们只是不将其设为掩码数组,而是将vmax kwarg指定为imshow:

If we simply don't make this a masked array, and instead specify the vmax kwarg to imshow:

import numpy as np
import matplotlib.pyplot as plt

threshold = 0.8
data = np.random.random((10,10))

fig, ax = plt.subplots()
im = ax.imshow(data, cmap=plt.cm.hot, interpolation='none', vmax=threshold)
cbar = fig.colorbar(im, extend='max')
cbar.cmap.set_over('green')

plt.show()

基本上,这是set_over(或以下)与set_bad之间的区别.

Basically, this is the difference between set_over (or under) and set_bad.

如果您仍然想使用带掩码的数组,则可以同时调用cbar.cmap.set_bad('green')set_over,这样您将获得所需的效果(尽管所有坏"值,而不仅仅是所有坏"值)阈值,将为绿色).如果采用该路线,则需要手动指定vmax.否则,它将被视为数组未遮罩部分的最大值.

If you did still want to use a masked array, you could just call cbar.cmap.set_bad('green') as well as set_over, and you'd get the effect you want (though all "bad" values, not just ones over the threshold, would be green). If you take that route, you'll need to manually specify the vmax. Otherwise it will be taken as the maximum of the unmasked portions of the array.

这篇关于热图上的特定离群值-matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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