Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

python - Adjust different transparency for different class in seaborn scatter plot

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")

Expecting Class 1 alpha to be 0.2.

Current Picture

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

enter image description here

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

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...