【问题标题】:Draw a point at the mean peak of a distplot or kdeplot in Seaborn在 Seaborn 中的 distplot 或 kdeplot 的平均峰值处绘制一个点
【发布时间】:2019-06-03 14:18:46
【问题描述】:

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

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

iris = sns.load_dataset("iris")
setosa = iris.loc[iris.species == "setosa"]
sns.kdeplot(setosa.sepal_width)

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

【问题讨论】:

    标签: pandas matplotlib seaborn


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-05-25
      • 2020-09-26
      • 2020-11-28
      • 2021-04-06
      • 2016-12-30
      • 1970-01-01
      • 2021-03-06
      • 2020-11-14
      • 2022-01-15
      相关资源
      最近更新 更多