Seaborn kde plot 绘制概率而不是密度(没有条形的 histplot)
Seaborn kde plot plotting probabilities instead of density (histplot without bars)
我有一个关于 seaborn 的问题 kdeplot
。在 histplot
中,可以设置他们想要的 stats(计数、频率、密度、概率),如果与 kde
参数一起使用,它也适用于 kdeplot
.但是,如果我只想获得带有概率的 kde 图估计,我还没有找到如何直接在 kdeplot
中更改它的方法。或者,如果可以关闭条形图,则 histplot
应该会产生相同的结果,但我也没有找到。那么如何才能做到这一点呢?
举一些直观的例子,我想要红色曲线,即。将参数传递给 kdeplot
以使用 probabilities
,或者从 histplot
:
中删除条形图
import seaborn
penguins = sns.load_dataset("penguins")
sns.histplot(data=penguins, x="flipper_length_mm", kde=True, stat="probability", color="r", label="probabilities")
sns.kdeplot(data=penguins, x="flipper_length_mm", color="k", label="kde density")
plt.legend()
非常感谢。
histplot
和 stat="probability"
的 y 轴对应于某个值属于某个柱的概率。最高条的 0.23
值意味着鳍状肢长度在 189.7
和 195.6
毫米之间(即特定箱子的边缘)的概率约为 23% .请注意,默认情况下,遇到的最小值和最大值之间分布有 10 个 bin。
kdeplot
的 y 轴类似于 probability density function。曲线的高度与值位于相应 x 值宽度 1
的区间内的近似概率成正比。 x=191
的 0.031
值表示长度在 190.5
和 191.5
之间的概率约为 3.1 %
。
现在,要直接获取 kdeplot
旁边的概率值,首先需要选择一个 bin 宽度。然后 y 值可以除以该 bin 以对应于该宽度的 bin 内的 x 值。 PercentageFormatter
提供了一种设置这种对应关系的方法,使用 ax.yaxis.set_major_formatter(PercentFormatter(1/binwidth))
.
下面的代码说明了一个 binwidth 为 5 mm
的示例,以及 histplot
如何匹配 kdeplot
。
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.ticker import PercentFormatter
fig, ax1 = plt.subplots()
penguins = sns.load_dataset("penguins")
binwidth = 5
sns.histplot(data=penguins, x="flipper_length_mm", kde=True, stat="probability", color="r", label="Probabilities",
binwidth=binwidth, ax=ax1)
ax2 = ax1.twinx()
sns.kdeplot(data=penguins, x="flipper_length_mm", color="k", label="kde density", ls=':', lw=5, ax=ax2)
ax2.set_ylim(0, ax1.get_ylim()[1] / binwidth) # similir limits on the y-axis to align the plots
ax2.yaxis.set_major_formatter(PercentFormatter(1 / binwidth)) # show axis such that 1/binwidth corresponds to 100%
ax2.set_ylabel(f'Probability for a bin width of {binwidth}')
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
plt.show()
PS:只显示kdeplot
的概率,代码可以是:
binwidth = 5
ax = sns.kdeplot(data=penguins, x="flipper_length_mm")
ax.yaxis.set_major_formatter(PercentFormatter(1 / binwidth)) # show axis such that 1/binwidth corresponds to 100%
ax.set_ylabel(f'Probability for a bin width of {binwidth}')
另一种选择是用 kde=True
绘制 histplot
,然后删除生成的柱。为了便于解释,应该设置 binwidth
。使用 binwidth=1
你会得到与密度图相同的 y 轴。 (kde_kws={'cut': 3})
让 kde 平滑地接近零,默认情况下 kde 曲线被数据的最小值和最大值截断)。
ax = sns.histplot(data=penguins, x="flipper_length_mm", binwidth=1, kde=True, stat='probability', kde_kws={'cut': 3})
ax.containers[0].remove() # remove the bars
ax.relim() # the axis limits need to be recalculated without the bars
ax.autoscale_view()
我有一个关于 seaborn 的问题 kdeplot
。在 histplot
中,可以设置他们想要的 stats(计数、频率、密度、概率),如果与 kde
参数一起使用,它也适用于 kdeplot
.但是,如果我只想获得带有概率的 kde 图估计,我还没有找到如何直接在 kdeplot
中更改它的方法。或者,如果可以关闭条形图,则 histplot
应该会产生相同的结果,但我也没有找到。那么如何才能做到这一点呢?
举一些直观的例子,我想要红色曲线,即。将参数传递给 kdeplot
以使用 probabilities
,或者从 histplot
:
import seaborn
penguins = sns.load_dataset("penguins")
sns.histplot(data=penguins, x="flipper_length_mm", kde=True, stat="probability", color="r", label="probabilities")
sns.kdeplot(data=penguins, x="flipper_length_mm", color="k", label="kde density")
plt.legend()
非常感谢。
histplot
和 stat="probability"
的 y 轴对应于某个值属于某个柱的概率。最高条的 0.23
值意味着鳍状肢长度在 189.7
和 195.6
毫米之间(即特定箱子的边缘)的概率约为 23% .请注意,默认情况下,遇到的最小值和最大值之间分布有 10 个 bin。
kdeplot
的 y 轴类似于 probability density function。曲线的高度与值位于相应 x 值宽度 1
的区间内的近似概率成正比。 x=191
的 0.031
值表示长度在 190.5
和 191.5
之间的概率约为 3.1 %
。
现在,要直接获取 kdeplot
旁边的概率值,首先需要选择一个 bin 宽度。然后 y 值可以除以该 bin 以对应于该宽度的 bin 内的 x 值。 PercentageFormatter
提供了一种设置这种对应关系的方法,使用 ax.yaxis.set_major_formatter(PercentFormatter(1/binwidth))
.
下面的代码说明了一个 binwidth 为 5 mm
的示例,以及 histplot
如何匹配 kdeplot
。
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.ticker import PercentFormatter
fig, ax1 = plt.subplots()
penguins = sns.load_dataset("penguins")
binwidth = 5
sns.histplot(data=penguins, x="flipper_length_mm", kde=True, stat="probability", color="r", label="Probabilities",
binwidth=binwidth, ax=ax1)
ax2 = ax1.twinx()
sns.kdeplot(data=penguins, x="flipper_length_mm", color="k", label="kde density", ls=':', lw=5, ax=ax2)
ax2.set_ylim(0, ax1.get_ylim()[1] / binwidth) # similir limits on the y-axis to align the plots
ax2.yaxis.set_major_formatter(PercentFormatter(1 / binwidth)) # show axis such that 1/binwidth corresponds to 100%
ax2.set_ylabel(f'Probability for a bin width of {binwidth}')
ax1.legend(loc='upper left')
ax2.legend(loc='upper right')
plt.show()
PS:只显示kdeplot
的概率,代码可以是:
binwidth = 5
ax = sns.kdeplot(data=penguins, x="flipper_length_mm")
ax.yaxis.set_major_formatter(PercentFormatter(1 / binwidth)) # show axis such that 1/binwidth corresponds to 100%
ax.set_ylabel(f'Probability for a bin width of {binwidth}')
另一种选择是用 kde=True
绘制 histplot
,然后删除生成的柱。为了便于解释,应该设置 binwidth
。使用 binwidth=1
你会得到与密度图相同的 y 轴。 (kde_kws={'cut': 3})
让 kde 平滑地接近零,默认情况下 kde 曲线被数据的最小值和最大值截断)。
ax = sns.histplot(data=penguins, x="flipper_length_mm", binwidth=1, kde=True, stat='probability', kde_kws={'cut': 3})
ax.containers[0].remove() # remove the bars
ax.relim() # the axis limits need to be recalculated without the bars
ax.autoscale_view()