'Weighted KDE for numpy array

weighted histogramI have two 1D numpy arrays and want to make a weighted histogram with a weighted KDE. The idea is to check the distribution of main gridded variable (VARIABLE 1)with respect to another gridded variable (VARIABLE 2), which I want to assign as weightsKDE WITHOUT WIEGHTS. Basically I want a KDE type plot with values of variable 1 binned on the x axis and values of variable 2(which I assign as weights) on the y scale.

I could only create the weighted histogram and not put a weighted KDE. I checked for weighted PDF but could not implement it properly either.

Heres's my code;

#Simple weighted histogram (graph 1)[![Simple Histogram][1]][1]:

import seaborn as sns
from scipy import stats
from seaborn import kdeplot

variable_1.astype(int)
fig, ax = plt.subplots()

ax.hist(varibale_1,  weights=variable_2)


#Using distplot - but without weights for KDE function (graph 2)[![Distplot with unweighted KDE][2]][2]
sns.distplot(variable_1, hist_kws={'weights': variable_2}, kde=True)

Also, in this distplot the y axis shows probabilities whereas I want values of the weighted variable.

I would appreciate any help in this regard.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source