简体   繁体   中英

Different point size based on hue argument in seaborn

I am trying to have different point sizes on a seaboard scatterplot depending on the value on the "hue" column of my dataframe.

sns.scatterplot(x="X", y="Y", data=df, hue='value',style='value')

value can take 3 different values (0,1 and 2) and I would like points which value is 2 to be bigger on the graph.

I tried the sizes argument : sizes=(1,1,4)

But could not get it done this way.

Let's use the s parameter and pass a list of sizes using a function of df['value'] to scale the point sizes:

df = pd.DataFrame({'X':[1,2,3],'Y':[1,4,9],'value':[1,0,2]})

import seaborn as sns

_ = sns.scatterplot(x='X',y='Y', data=df, s=df['value']*50+10)

Output: 在此处输入图片说明

Using seaborn scatterplots arguments:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({'X':[1,2,3,4,5],'Y':[1,2,3,4,5],
                   'value':[1,1,0,2,2]})

df["size"] = np.where(df["value"] == 2, "Big", "Small")

sns.scatterplot(x="X", y="Y", hue='value', size="size",
                data=df, size_order=("Small", "Big"), sizes=(160, 40))

plt.show()

在此处输入图片说明

Note that the order of sizes needs to be reveresed compared to the size_order . I have no idea why that would make sense, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM