在seaborn散点图中为不同的类调整不同的透明度 [英] Adjust different transparency for different class in seaborn scatter plot

查看:78
本文介绍了在seaborn散点图中为不同的类调整不同的透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望散点图中的不同类具有不同的alpha值(透明度).

I want different alpha value (transparency) for Different Class in scatter plot.

sns.scatterplot(x="BorrowerAPR", y="LoanOriginalAmount", data=df_new, 
                alpha=0.03, hue="LoanStatus")

期望 Class 1 alpha 为 0.2.

Expecting Class 1 alpha to be 0.2.

推荐答案

一种方法是单独绘制它们,但如果未指定,您将获得不同的色调.以下是内置的 tips 数据集的示例,其中烟民和非烟民使用不同的 alpha 值:

One way is to plot them separately, though you'll get different hues if not specified. Here's an example from the built-in tips dataset with different alpha values for smokers and non-smokers:

import seaborn as sns
import numpy as np

tips = sns.load_dataset("tips")
tips["alpha"] = np.where(tips.smoker == "Yes", 1.0, 0.5)

ax = sns.scatterplot(x="total_bill", y="tip",
                     data=tips[tips.alpha == 0.5], alpha=0.5)
sns.scatterplot(x="total_bill", y="tip", data=tips[tips.alpha == 1.0], 
                alpha=1.0, ax=ax)

这也将较高的 alpha 点堆叠在较低的点上.

This also stacks the higher-alpha points atop the lower ones.

更一般地,用于多个 alpha 类别:

More generally for multiple alpha categories:

alphas = tips.alpha.sort_values().unique()
ax = sns.scatterplot(x="total_bill", y="tip",
                     data=tips[tips.alpha == alphas[0]], alpha=alphas[0])
for alpha in alphas[1:]:
    sns.scatterplot(x="total_bill", y="tip",
                    data=tips[tips.alpha == alpha], alpha=alpha, ax=ax)

这篇关于在seaborn散点图中为不同的类调整不同的透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆