gpt4 book ai didi

pandas - 在 Seaborn 的 distplot 或 kdeplot 的平均峰值处画一个点

转载 作者:行者123 更新时间:2023-12-02 00:24:59 25 4
gpt4 key购买 nike

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

例如,下面生成的 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 统计量。

最佳答案

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

关于pandas - 在 Seaborn 的 distplot 或 kdeplot 的平均峰值处画一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54099435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com