How to control the legend in Seaborn - Python

后端 未结 2 990
渐次进展
渐次进展 2021-01-14 06:15

I am trying to find guidance on how to control and customize the legend in Seaborn plots but I can not find any.

To make the issue more concrete I provide a reproduc

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-14 07:10

    I've always found changing labels in seaborn plots once they are created to be a bit tricky. The easiest solution seems to be to change the input data itself, by mapping the values and column names. You can create a new dataframe as follows, then use the same plot commands.

    data = surveys_by_year_sex_long.rename(columns={'sex': 'Sex'})
    data['Sex'] = data['Sex'].map({'M': 'Male', 'F': 'Female'})
    sn.factorplot(
        x = "year", y = "wgt", data = data, hue = "Sex",
        kind = "bar", legend_out = True,
        palette = sn.color_palette(palette = ["SteelBlue" , "Salmon"]),
        hue_order = ["Male", "Female"])
    

    Hopefully this does what you need. The potential problem is that if the dataset is large, creating a whole new dataframe in this manner adds some overhead.

提交回复
热议问题