Python 从Seaborn distplot获取数据点

Python 从Seaborn distplot获取数据点,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我用 绘制观测值的单变量分布图。不过,我不仅需要图表,还需要数据点。如何从matplotlib轴(由distplot返回)获取数据点?您可以使用。例如,要获取第一行: sns.distplot 这将返回两个numpy数组,其中包含该行的x和y值 对于条形图,信息存储在: sns.distplot(x).get_lines()[0].get_data() 您可以通过以下功能访问杆的高度: 如果要获取直方图的kde值,可以使用scikit学习KernelDensity函数: [h.get_he

我用

绘制观测值的单变量分布图。不过,我不仅需要图表,还需要数据点。如何从matplotlib轴(由distplot返回)获取数据点?

您可以使用。例如,要获取第一行:

sns.distplot 
这将返回两个numpy数组,其中包含该行的x和y值

对于条形图,信息存储在:

sns.distplot(x).get_lines()[0].get_data()
您可以通过以下功能访问杆的高度:


如果要获取直方图的kde值,可以使用scikit学习
KernelDensity
函数:

[h.get_height() for h in sns.distplot(x).patches]

这不是严格可靠的。如果在调用
distplot
之前,
轴上有任何行,您将从该行获取数据。另一个提示:要获取箱子的左边缘、宽度和高度,请执行以下操作:
l=[[h.xy[0],h.get_width(),h.get_height()]在sns.distplot(x.patches]中表示h
我刚刚测试了解决方案,但它不适用于我,因为“get_lines()”不是FacetGrid对象的有效方法。我成功地使用了这个答案:
[h.get_height() for h in sns.distplot(x).patches]
import numpy as np
import pandas as pd
from sklearn.neighbors import KernelDensity

ds=pd.read_csv('data-to-plot.csv')
X=ds.loc[:,'Money-Spent'].values[:, np.newaxis]


kde = KernelDensity(kernel='gaussian', bandwidth=0.75).fit(X) #you can supply a bandwidth
                                                              #parameter. 

x=np.linspace(0,5,100)[:, np.newaxis]

log_density_values=kde.score_samples(x)
density=np.exp(log_density)

array([1.88878660e-05, 2.04872903e-05, 2.21864649e-05, 2.39885206e-05,
       2.58965064e-05, 2.79134003e-05, 3.00421245e-05, 3.22855645e-05,
       3.46465903e-05, 3.71280791e-05, 3.97329392e-05, 4.24641320e-05,
       4.53246933e-05, 4.83177514e-05, 5.14465430e-05, 5.47144252e-05,
       5.81248850e-05, 6.16815472e-05, 6.53881807e-05, 6.92487062e-05,
       7.32672057e-05, 7.74479375e-05, 8.17953578e-05, 8.63141507e-05,
       ..........................
       ..........................
       3.93779919e-03, 4.15788216e-03, 4.38513011e-03, 4.61925890e-03,
       4.85992626e-03, 5.10672757e-03, 5.35919187e-03, 5.61677855e-03])