我正在尝试使用带有不同颜色的某些正方形在seaborn(python)中创建一个热图,(这些正方形包含无关紧要的数据-在我的情况下,它将是值小于1.3的正方形,即-p值的-log > 0.05)。我找不到这样的功能。掩盖这些方块也没有用。
这是我的代码:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import seaborn as sns; sns.set()
data = [[1.3531363408, 3.339479161, 0.0760855365], [5.1167382617, 3.2890920405, 2.4764601828], [0.0025058257, 2.3165128345, 1.6532714962], [0.2600549869, 5.8427407219, 6.6627226609], [3.0828581725, 16.3825494439, 12.6722666929], [2.3386307357, 13.7275065772, 12.5760972276], [1.224683813, 2.2213656372, 0.6300876451], [0.4163788387, 1.8128374089, 0.0013106046], [0.0277592882, 2.9286203949, 0.810978992], [0.0086613622, 0.6181261247, 1.8287878837], [1.0174519889, 0.2621290291, 0.1922637697], [3.4687429571, 4.0061981716, 0.5507951444], [7.4201304939, 3.881457516, 0.1294141768], [2.5227546319, 6.0526491816, 0.3814362442], [8.147538027, 14.0975727815, 7.9755706939]]
cmap2 = mpl.colors.ListedColormap(sns.cubehelix_palette(n_colors=20, start=0, rot=0.4, gamma=1, hue=0.8, light=0.85, dark=0.15, reverse=False))
ax = sns.heatmap(data, cmap=cmap2, vmin=0)
plt.show()

我想补充一点,我不是很高级的程序员。

最佳答案

好的,现在我可以自己回答问题了:)这是解决问题的代码:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import seaborn as sns; sns.set()
data = np.array([[1.3531363408, 3.339479161, 0.0760855365], [5.1167382617, 3.2890920405, 2.4764601828], [0.0025058257, 2.3165128345, 1.6532714962], [0.2600549869, 5.8427407219, 6.6627226609], [3.0828581725, 16.3825494439, 12.6722666929], [2.3386307357, 13.7275065772, 12.5760972276], [1.224683813, 2.2213656372, 0.6300876451], [0.4163788387, 1.8128374089, 0.0013106046], [0.0277592882, 2.9286203949, 0.810978992], [0.0086613622, 0.6181261247, 1.8287878837], [1.0174519889, 0.2621290291, 0.1922637697], [3.4687429571, 4.0061981716, 0.5507951444], [7.4201304939, 3.881457516, 0.1294141768], [2.5227546319, 6.0526491816, 0.3814362442], [8.147538027, 14.0975727815, 7.9755706939]])
cmap1 = mpl.colors.ListedColormap(['c'])
sns.heatmap(data)
sns.heatmap(data, mask=data > 1.3, cmap=cmap1, cbar=False)
plt.show()

因此,以前不起作用的屏蔽问题是它仅适用于列表中没有的数组。
另一件事是两次绘制热图,第二次使用蒙版。
我唯一仍然不明白的是,它掩盖了所写内容的相反字段。.我想掩盖低于1.3的值,但随后将其着色成高于1.3的值。所以我写了mask = data> 1.3,现在它可以工作了...

关于python - 如何更改海洋热图中某些正方形的颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33125304/

10-15 22:53