如何在 Seaborn 中屏蔽两个不同范围的值
How do I mask two different ranges of values in Seaborn
所以我希望将热图中介于 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'
用上面的代码编辑我得到:
如 this answer 中所述,对于 Pandas 中的逐元素布尔比较,您需要使用 &
和 |
,并将每个条件括在括号中。所以要结合你的三个条件,你需要
mask=(X<2.3e-6) | (0.05<X) | mask.astype(bool),
所以我希望将热图中介于 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'
用上面的代码编辑我得到:
如 this answer 中所述,对于 Pandas 中的逐元素布尔比较,您需要使用 &
和 |
,并将每个条件括在括号中。所以要结合你的三个条件,你需要
mask=(X<2.3e-6) | (0.05<X) | mask.astype(bool),