简体   繁体   中英

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

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.

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)

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

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.

In modern seaborn use the option binrange of histplot (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM