模拟已弃用的seaborn distplots [英] Emulating deprecated seaborn distplots

查看:66
本文介绍了模拟已弃用的seaborn distplots的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Seaborn

那么,我们如何配置 histplot 来复制已弃用的 distplot 的输出?

解决方案

因为我花了一些时间在这个上,所以我想我分享这个以便其他人可以轻松地适应这种方法:

from matplotlib import pyplot as plt将熊猫导入为 pd将 seaborn 作为 sns 导入将 numpy 导入为 npx_list = [1, 2, 3, 4, 6, 7, 9, 9, 9, 10]df = pd.DataFrame({X": x_list, Y": range(len(x_list))})f, (ax_dist, ax_hist) = plt.subplots(2, sharex=True)sns.distplot(df[X"], ax=ax_dist)ax_dist.set_title(旧的distplot")_, FD_bins = np.histogram(x_list, bins=fd")bin_nr = min(len(FD_bins)-1, 50)sns.histplot(数据=df,x=X",ax=ax_hist,bins=bin_nr,stat=密度",alpha=0.4,kde=True,kde_kws={cut":3})ax_hist.set_title(新的直方图")plt.show()

样本输出:

主要变化是

  • bins=bin_nr - 使用 弗里德曼Diaconis Estimator 并将上限限制为 50
  • stat=密度" - 在直方图中显示密度而不是计数
  • alpha=0.4 - 相同的透明度
  • kde=True - 添加核密度图
  • kde_kws={cut": 3} - 将核密度图扩展到直方图限制之外

关于 bins="fd" 的 bin 估计,我不确定这确实是 distplot 使用的方法.非常欢迎评论和更正.

我删除了 **{linewidth": 0} 因为 distplot 有,正如@mwaskom 在评论中指出的那样,edgecolor可以由 matplotlib 设置为默认 facecolor 的直方图条周围的 code> 线.所以,你必须根据你的风格偏好来解决这个问题.

Seaborn distplot is now deprecated and will be removed in a future version. It is suggested to use histplot (or displot as a figure-level plot) as an alternative. But the presets differ between distplot and histplot:

from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns

x_list = [1, 2, 3, 4, 6, 7, 9, 9, 9, 10]
df = pd.DataFrame({"X": x_list, "Y": range(len(x_list))})

f, (ax_dist, ax_hist) = plt.subplots(2, sharex=True)

sns.distplot(df["X"], ax=ax_dist)
ax_dist.set_title("old distplot")
sns.histplot(data=df, x="X", ax=ax_hist)
ax_hist.set_title("new histplot")

plt.show()

So, how do we have to configure histplot to replicate the output of the deprecated distplot?

解决方案

Since I spent some time on this, I thought I share this so that others can easily adapt this approach:

from matplotlib import pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np

x_list = [1, 2, 3, 4, 6, 7, 9, 9, 9, 10]
df = pd.DataFrame({"X": x_list, "Y": range(len(x_list))})

f, (ax_dist, ax_hist) = plt.subplots(2, sharex=True)

sns.distplot(df["X"], ax=ax_dist)
ax_dist.set_title("old distplot")
_, FD_bins = np.histogram(x_list, bins="fd")
bin_nr = min(len(FD_bins)-1, 50)
sns.histplot(data=df, x="X", ax=ax_hist, bins=bin_nr, stat="density", alpha=0.4, kde=True, kde_kws={"cut": 3})
ax_hist.set_title("new histplot")

plt.show()

Sample output:

The main changes are

  • bins=bin_nr - determine the histogram bins using the Freedman Diaconis Estimator and restrict the upper limit to 50
  • stat="density" - show density instead of count in the histogram
  • alpha=0.4 - for the same transparency
  • kde=True - add a kernel density plot
  • kde_kws={"cut": 3} - extend the kernel density plot beyond the histogram limits

Regarding the bin estimation with bins="fd", I am not sure that this is indeed the method used by distplot. Comments and corrections are more than welcome.

I removed **{"linewidth": 0} because distplot has, as pointed out by @mwaskom in a comment, an edgecolor line around the histogram bars that can be set by matplotlib to the default facecolor. So, you have to sort this out according to your style preferences.

这篇关于模拟已弃用的seaborn distplots的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆