Does Seaborn distplot not support a range?

Question:

I have an array of data, called data1, that contains values from 0 to more than a thousand. I only want to have a histogram and a KDE of those values from 0 to 10. Hence I wrote:

sns.distplot(data1, kde=True, hist=True, hist_kws={"range": [0,10]})
plt.show()

What I get however is a histogram of all values (well into 2000s).

Asked By: Ben

||

Answers:

You could just filter your data and call displot over the filtered data:

filtered = data1[(data1 >= 0) & (data1 < 10)]
sns.distplot(filtered, kde=True, hist=True, hist_kws={"range": [0,10]})
plt.show()

Assuming data1 is a numpy array.

Answered By: Imanol Luengo

It does, just put plt.xlim(x,x1) in a line after declaring the plot and the resultant plot would only have the x values between x and x1. You can do the same for the y axis using ylim.

Answered By: ybindal

If you want the KDE and histogram to be computed only for the values in [0,10] you can use the arguments kde_kws={"clip":(0,10)}, hist_kws={"range":(0,10)}:

sns.distplot(data1, kde=True, hist=True, kde_kws={"clip":(0,10)}, hist_kws={"range":(0,10)})
plt.show()
Answered By: Ale

You can set a range for Axes object that sns returns.

ax = sns.distplot(data1, kde=True, hist=True, hist_kws={"range": [0,10]})
ax.set_xlim(0, 10)
Answered By: Sobir

Use the option binrange of histplot.

This works in modern seaborn too (note that **distplot is depreciated).

binrange: pair of numbers or a pair of pairs
Lowest and highest value for bin edges; 
can be used either with bins or binwidth. Defaults to data extremes.

Answered By: Maciej S.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.