在Seaborn中的distplot或kdeplot的平均峰上绘制点

时间:2019-01-08 20:46:37

标签: pandas matplotlib 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)

enter image description here

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

1 个答案:

答案 0 :(得分:2)

这是一种方法。这里的想法是首先提取图中线对象的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)

enter image description here