I'm trying to get the points from a KDE plot in order to send them via API so the plot can be displayed via frontend. For example, if I have the following data:
df = pd.DataFrame({'x': [3000.0,
2897.0,
4100.0,
2539.28,
5000.0,
3615.0,
2562.05,
2535.0,
2413.0,
2246.0],
'y': [1, 2, 1, 1, 1, 2, 1, 3, 1, 1]})
import seaborn as sns
sns.kdeplot(x=df['x'], weights=df['y'])
And I plot it using seaborn kdeplot it gives me this plot:
Now I wanted to send some points of this plot via an API. My idea was to use KernelDensity from sklearn to estimate the density of some points. So I used this code:
from sklearn.neighbors import KernelDensity
x_points = np.linspace(0, df['x'].max(), 30)
kde = KernelDensity()
kde.fit(df['x'].values.reshape(-1, 1), sample_weight=df['y'])
logprob = kde.score_samples(x_points.reshape(-1, 1))
new_df = pd.DataFrame({'x': x_points, 'y': np.exp(logprob)})
Which, if I plot using a lineplot, doesn't look anything like seaborn kdeplot.
My question is: Given a dataframe and the kdeplot shown, how can I get the probability of some point x in this plot?
EDIT: Adding code to plot sns.kdeplot