In Python, I am attempting to find a way to plot/rescale kde's so that they match up with the histograms of the data that they are fitted to:
The above is a nice example of what I am going for, but for some data sources , the scaling gets completely screwed up, and you get the following results, coming from the following code:
import numpy as np
import matplotlib as plot
import seaborn as sns
x1 = np.array([0.0, 0.0, 0.0, 0.0, 0.5, -0.12500000000000003, 0.0, -0.4, 0.0, 0.25])
## Simple histogram, weighted to reveal probabilities
plt.hist(x1, weights=np.ones(len(x1))/len(x1));
## Histogram + kde, but clearly something has gone wrong
sns.distplot(x1, hist=True, kde=True)
Correct histogram:
Incorrect histogram + kde scaling:
Now, I am aware that kernel density estimators are "meant" to integrate over 1, or have the area beneath them equal to one (as recounted here, and many other answers on stack exchange), so they do not necessarily have to line up with a weighted histogram.
However, I find that a kde that does line up with a histogram would be much more informative, revealing a best estimate as to how the histogram really looks (if more data were simply available).
Is there a way to do this? To consistently get a kde image that looks like the top one? I will be extremely appreciative of any help on this.