【问题标题】:How do you create a legend for kde plot in Seaborn?你如何在 Seaborn 中为 kde 情节创造传奇?
【发布时间】:2021-01-08 21:00:12
【问题描述】:

我有一个 kdeplot,但我正在努力弄清楚如何创建图例。

import matplotlib.patches as mpatches  # see the tutorial for how we use mpatches to generate this figure!

# Set 'is_workingday' to a boolean array that is true for all working_days
is_workingday = daily_counts["workingday"] == "yes"
is_not_workingday = daily_counts['workingday'] == "no"
# Bivariate KDEs require two data inputs. 
# In this case, we will need the daily counts for casual and registered riders on workdays
casual_workday = daily_counts.loc[is_workingday, 'casual']
registered_workday = daily_counts.loc[is_workingday, 'registered']

# Use sns.kdeplot on the two variables above to plot the bivariate KDE for weekday rides
sns.kdeplot(casual_workday, registered_workday, color = "red", cmap = "Reds", hue = "workingday", legend = True)

# Repeat the same steps above but for rows corresponding to non-workingdays
casual_non_workday =  daily_counts.loc[is_not_workingday, 'casual']
registered_non_workday = daily_counts.loc[is_not_workingday, 'registered']


# Use sns.kdeplot on the two variables above to plot the bivariate KDE for non-workingday rides
sns.kdeplot(casual_non_workday, registered_non_workday, color = 'blue', cmap = "Blues", legend = True, shade = False)

得到我这个:

我试图得到这个:

【问题讨论】:

    标签: python plot seaborn legend


    【解决方案1】:

    一种方法是将label= 传递给kdeplot,然后请求显示图例。

    geyser = sns.load_dataset("geyser")
    long = geyser.loc[geyser['kind']=='long']
    short = geyser.loc[geyser['kind']=='short']
    sns.kdeplot(x=long["waiting"], y=long["duration"], label='long')
    sns.kdeplot(x=short["waiting"], y=short["duration"], label='short')
    plt.legend()
    

    另一种方法是使用 seaborn 必须基于 hue= 列拆分数据帧的内置方式。在你的情况下,它看起来像下面,但不知道你的数据框的结构,不可能确定。请参阅the documentation 了解更多信息。

    sns.kdeplot(x='casual', y='registered', hue='workingday', data=daily_counts, shade=False, legend=True)
    

    【讨论】:

      【解决方案2】:

      当每个 kdeplot 使用一种颜色时,另一个答案效果很好。

      如果使用诸如“红色”之类的颜色图,则会显示非常浅的红色。自定义颜色图可以显示范围中间的颜色:

      from matplotlib import pyplot as plt
      import matplotlib.patches as  mpatches
      import seaborn as sns
      import numpy as np
      
      casual_workday = np.random.randn(100) * 1.2
      registered_workday = 0.8 * np.random.randn(100) + casual_workday * 0.2 + 1
      sns.kdeplot(x=casual_workday, y=registered_workday, color="red", cmap="Reds", shade=False)
      
      casual_non_workday = np.random.randn(100) * 1.6
      registered_non_workday = 0.5 * np.random.randn(100) + casual_non_workday * 0.5 - 1
      sns.kdeplot(x=casual_non_workday, y=registered_non_workday, cmap="Blues", shade=False)
      
      handles = [mpatches.Patch(facecolor=plt.cm.Reds(100), label="Workday"),
                 mpatches.Patch(facecolor=plt.cm.Blues(100), label="Non-workday")]
      plt.legend(handles=handles)
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-14
        • 1970-01-01
        • 2021-09-06
        • 2021-02-10
        • 2020-11-11
        • 2017-01-05
        • 1970-01-01
        相关资源
        最近更新 更多