【发布时间】:2021-10-24 06:49:46
【问题描述】:
所以我希望将热图中 2.3e-6-0.05 之间的值涂成红色。我想通过在另一个上绘制一个热图来做到这一点。但我似乎无法找到一种方法来掩盖不同值的数量。这是我的尝试。
from scipy.stats import pearsonr
N = 10
data = np.random.uniform(0, 45, size=(N, N))
for x, y in np.random.randint(0, N, 50).reshape(-1, 2):
data[x, y] = np.nan # fill in some nans at random places
df = pd.DataFrame(data)
def pearsonr_pval(x,y):
return pearsonr(x,y)[1]
data = df.loc[:, (df != 0).any(axis=0)]
data = data.iloc[:,3:50]
to_log = data.columns
df_log = data[to_log].applymap(lambda x: np.log(x+1))
X = df_log.corr(method = pearsonr_pval)
sns.set_style("darkgrid")
mask = np.zeros_like(X)
mask[np.triu_indices_from(mask)] = True
with sns.axes_style("white"):
f, ax = plt.subplots(figsize=(20, 20))
ax = sns.heatmap(X,
mask=mask,
vmax=1,
vmin=0,
square=True,
cmap="YlGnBu",
annot_kws={"size": 1})
ax = sns.heatmap(X,
mask=(X.values<2.3e-6) & (0.05<X.values) & mask.astype(bool),
vmax=1,
vmin=0,
square=True,
cmap="rocket",
annot_kws={"size": 1})
但我得到一个错误:TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'
【问题讨论】:
-
谢谢!我尝试添加 mask= (X.values
-
它不会掩盖上三角 :(,公平地说,它不会掩盖任何东西。我会展示我的情节
-
非常感谢!把它作为答案,这样我就可以投票了:)
-
好的。顺便说一句,
values转换我错了,没有必要 - 在我的答案中更新它
标签: python-3.x seaborn