【问题标题】:legends on seaborn kde plotseaborn kde情节上的传说
【发布时间】:2020-03-14 12:54:35
【问题描述】:

我不知道如何在 seaborn kde 情节中添加图例。

uses = df.primary_use.unique()
plt.figure(figsize=(7,7))
ax = plt.axes()
plt.legend(uses)
for use in uses: 
    sns.kdeplot(df[df['primary_use'] == use]['wind_speed'], ax=ax)

在此处查看输出:

uses 数组看起来像:

array(['Education', 'Lodging/residential', 'Office',
       'Entertainment/public assembly', 'Public services'], dtype=object)

uses 是传说中的样子,但它为每个情节添加了wind_speed

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    试试这个,使用label参数:

    for use in uses: 
        sns.kdeplot(df[df['primary_use'] == use]['wind_speed'], ax=ax, label=use)
    

    我的 MCVE:

    df = pd.DataFrame(index=np.random.choice(['a','b','c'], 100), data=np.random.randint(0,100,(100)))
    df=df.rename_axis('use').reset_index()
    
    fig, ax = plt.subplots()
    use=['a','b','c']
    for u in use:
        sns.kdeplot(df[df['use'] == u][0], ax=ax, label=u)
    

    输出:

    【讨论】:

      猜你喜欢
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 2019-07-13
      • 1970-01-01
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多