Mr.*_*. T 4 python matplotlib histogram kernel-density seaborn
Seaborndistplot
现已弃用,并将在未来版本中删除。建议使用histplot
(或displot
作为图形级图)作为替代方案。distplot
但和之间的预设有所不同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()
Run Code Online (Sandbox Code Playgroud)
那么,我们如何配置histplot
来复制已弃用的输出distplot
?
由于我在这方面花了一些时间,我想我分享这个以便其他人可以轻松地采用这种方法:
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()
Run Code Online (Sandbox Code Playgroud)
主要变化是
bins=bin_nr
- 使用Freedman Diaconis 估计器确定直方图箱并将上限限制为 50stat="density"
- 在直方图中显示密度而不是计数alpha=0.4
- 相同的透明度kde=True
- 添加核密度图kde_kws={"cut": 3}
- 将核密度图扩展到直方图限制之外关于 的 bin 估计bins="fd"
,我不确定这确实是 所使用的方法distplot
。非常欢迎评论和更正。
我删除了,**{"linewidth": 0}
因为distplot
正如 @mwaskom 在评论中指出的那样,edgecolor
直方图条周围有一条线,可以由 matplotlib 设置为默认值facecolor
。所以,你必须根据你的风格偏好来解决这个问题。
归档时间: |
|
查看次数: |
3261 次 |
最近记录: |