[英]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. value可以采用3个不同的值(0,1和2),我希望点值为2的点在图表上更大。
I tried the sizes argument : sizes=(1,1,4) 我尝试过sizes参数: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: 让我们使用s
参数,并使用df ['value']函数传递尺寸列表以缩放点尺寸:
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)
Using seaborn scatterplots
arguments: 使用seaborn的scatterplots
参数:
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
. 请注意,与size_order
相比, sizes
顺序需要重新size_order
。 I have no idea why that would make sense, though. 不过,我不知道为什么这样做会有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.