简体   繁体   中英

Set seaborn relplot legend - hue values

I have the analysis with only 3 values in dataframe which are set for relplot "hue" parameter, but seaborn plots some auto-range hue legend with five values. How I can specify exact values?

sns.relplot(x='x', y='y', 
         hue = 'hue', style='style',
         kind='scatter', size = 'size', 
         data=df2)

 size style hue col yx 8 3.717546 1:1 50 715 1.930280e+06 4.932825e+06 24 7.165478 1:2 50 715 1.712680e+06 7.112875e+06 40 5.357067 2:1 50 715 1.148624e+06 2.494125e+06 56 2.758713 1:1 50 715 1.766268e+06 3.196175e+06 72 3.402869 1:2 50 715 9.385742e+05 4.046025e+06 88 3.160937 2:1 50 715 9.856859e+05 1.585155e+06 104 4.094309 1:1 250 715 8.416346e+06 7.944250e+07 120 39.287651 1:2 250 715 6.217049e+06 1.352370e+08 132 1.625882 1:1 5 715 5.055178e+04 5.062150e+04 148 35.241220 2:1 250 715 6.171461e+06 4.046025e+07 164 2.071234 1:1 250 715 6.038509e+06 2.697350e+07 180 13.498847 1:2 250 715 4.636012e+06 4.951300e+07 196 9.499857 2:1 250 715 4.012017e+06 1.322810e+07 212 4.166135 1:1 5 1545 6.218541e+05 6.244550e+05 228 4.979377 1:2 5 1545 6.864812e+05 7.316100e+05 244 4.127152 2:1 5 1545 3.558532e+05 3.565675e+05 257 2.516927 1:2 5 715 5.884268e+04 5.893525e+04 271 4.942125 1:1 5 1545 4.836641e+05 4.840450e+05

so, there are 3 different values for, "style", "hue" and "col" and the legend shows values that never appeared in the df2, At least I would like to fix hue legend to there values: [5, 50, 250]

这就是情节的样子

In your case, seaborn thinks that hue is a continuous variable. You need to change that. One way is to change the dataframe, for example, by converting the hue column to strings.

df['hue'] = df['hue'].map(lambda x: str(x))

Alternatively, one may enforce seaborn to treat "hue" as a categorical random variable directly (but it acts glitchy in my experience). Please insert to relplot keywords.

hue_order = np.unique(df['hue'])

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