In maptplotlib, one can create a heatmap representation of a correlation matrix using the imshow function. By definition, such a matrix is symmetrical around its main diagonal, therefore there is no need to present both the upper and lower triangles. For example:
(source: wisc.edu https://yin.che.wisc.edu/images/ProteinMatrix.jpg)
上面的例子取自这个网站 https://katesmaps.blogspot.com/2008/08/correlation-matrix.html不幸的是,我不知道如何在 matplotlib 中做到这一点。将矩阵的上/下部分设置为 None 会产生黑色三角形。我用谷歌搜索“matplotlib缺失值”,但找不到任何有用的东西
道格提供的答案的问题在于它依赖于颜色图将零值映射到白色的事实。这意味着不包含白色的颜色图没有用。解决的关键是cm.set_bad
功能。您可以使用 None 或 NumPy 屏蔽数组屏蔽矩阵中不需要的部分,set_bad
为白色,而不是默认的黑色。采用 doug 的例子,我们得到以下结果:
import numpy as NP
from matplotlib import pyplot as PLT
from matplotlib import cm as CM
A = NP.random.randint(10, 100, 100).reshape(10, 10)
mask = NP.tri(A.shape[0], k=-1)
A = NP.ma.array(A, mask=mask) # mask out the lower triangle
fig = PLT.figure()
ax1 = fig.add_subplot(111)
cmap = CM.get_cmap('jet', 10) # jet doesn't have white color
cmap.set_bad('w') # default value is 'k'
ax1.imshow(A, interpolation="nearest", cmap=cmap)
ax1.grid(True)
PLT.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)