在 Seaborn 中的 distplot 或 kdeplot 的平均峰值处绘制一个点

Tho*_*hew 2 matplotlib pandas seaborn

我感兴趣的是自动绘制分布平均峰值上方的点,由 kdeplot 或带有 kde 的 distplot 表示。手动绘制点和线很简单,但我很难推导这个最大坐标点。

例如,下面生成的 kdeplot 应该在大约 (3.5, 1.0) 处绘制一个点:

iris = sns.load_dataset("iris")
setosa = iris.loc[iris.species == "setosa"]
sns.kdeplot(setosa.sepal_width)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这个问题的最终目标是画一条线穿过下一个峰值(一个图中的两个分布),并在其上方打印 t 统计量。

She*_*ore 6

这是一种方法。这里的想法是首先提取图中线条对象的 x 和 y 数据。然后,获取峰值的 id,最后绘制与分布峰值相对应的单个 (x,y) 点。

import numpy as np
import seaborn as sns
iris = sns.load_dataset("iris")
setosa = iris.loc[iris.species == "setosa"]
ax = sns.kdeplot(setosa.sepal_width)

x = ax.lines[0].get_xdata() # Get the x data of the distribution
y = ax.lines[0].get_ydata() # Get the y data of the distribution
maxid = np.argmax(y) # The id of the peak (maximum of y data)
plt.plot(x[maxid],y[maxid], 'bo', ms=10)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述