Ben
Ben

Reputation: 475

Does Seaborn distplot not support a range?

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).

Upvotes: 17

Views: 24269

Answers (5)

Maciej Skorski
Maciej Skorski

Reputation: 3354

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.

Upvotes: 2

Sobir
Sobir

Reputation: 165

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)

Upvotes: 4

Ale
Ale

Reputation: 1004

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()

Upvotes: 1

ybindal
ybindal

Reputation: 11

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.

Upvotes: 1

Imanol Luengo
Imanol Luengo

Reputation: 15889

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.

Upvotes: 14

Related Questions