【问题标题】:Updating Seaborn distplot code to version 0.11将 Seaborn distplot 代码更新到 0.11 版
【发布时间】:2021-01-30 21:17:12
【问题描述】:

使用 distplot 绘制直方图

sns.distplot(a, color="red", label="100% Equities")

在 Seaborn 0.11 或更高版本下运行会产生警告,表明 distplot 将被弃用并改用 displot。

使用 displot 作为直接替换(只需将函数名称从 distplot 更改为 displot)不会产生相同的直方图。

替换代码是什么?

【问题讨论】:

  • label 似乎不是 displot 的参数:seaborn.pydata.org/generated/seaborn.displot.html
  • 上面的代码是针对 distplot(不是 displot)。 distplot 是已弃用的代码,它确实有一个标签。 histplot(见下面的答案)也确实从 bar 继承标签。如果你在你给出的链接中追查 kwargs(选择 histplot,然后在最后选择栏 - 你会发现 label 关键字可用。)Matplotlib 做了很多继承:-)
  • BTW 这是在尝试使用已弃用的代码时发出的警告消息。 FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).

标签: python seaborn


【解决方案1】:

使用

  • histplot 而不是 distplot
  • 并添加关键字 args kde=True, stat="density", linewidth=0

所以:

sns.histplot(a, color="red", label="100% Equities", kde=True, stat="density", linewidth=0)

替换

sns.distplot(a, color="red", label="100% Equities")

【讨论】:

    【解决方案2】:

    Hisplot 的代码示例,用于显示机器在每个点的第一状态和零状态的频率:

    feature2_ok = df.loc[df["target"] == 1]
    
    feature2_ng = df.loc[df["target"] == 0]
    
    fig, ax = plt.subplots(figsize=(20, 6))
    sns.histplot(feature2_ok["feature_2"], color="orange", label="100% Equities", kde=True, linewidth=0)
    
    sns.histplot(feature2_ng["feature_2"], label="100% Equities", kde=True, linewidth=0)
    
    ax.set(xlim=(-100, 700), 
     xticks=[-100, -50, 0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700])
    
    plt.legend(["Frist State", "Zero State"])
    
    plt.title('Machine Performance Feature 2')
    
    plt.ylabel('Frequency')
    
    plt.grid()
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2019-11-15
      • 1970-01-01
      • 2016-08-05
      • 2019-08-03
      • 2021-11-26
      • 2017-10-12
      • 2015-10-20
      • 2019-11-01
      • 1970-01-01
      相关资源
      最近更新 更多