【问题标题】:How do I mask two different ranges of values in Seaborn如何在 Seaborn 中屏蔽两个不同的值范围
【发布时间】: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


【解决方案1】:

this answer 中所述,对于 Pandas 中的元素方式布尔比较,您需要使用 &amp;|,并将每个条件括在括号中。所以要结合你的三个条件,你需要

mask=(X<2.3e-6) | (0.05<X) | mask.astype(bool),

【讨论】:

  • 哈!这行不通。另一方面掩码=(X 这样做
  • 好的,我认为您的问题不清楚 - 已更新
猜你喜欢
  • 2015-12-09
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 1970-01-01
  • 1970-01-01
  • 2022-07-01
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多