[英]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.我有一个数据数组,称为 data1,其中包含从 0 到超过一千的值。 I only want to have a histogram and a KDE of those values from 0 to 10. Hence I wrote:
我只想有一个直方图和一个从 0 到 10 的值的 KDE。因此我写道:
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).然而,我得到的是所有值的直方图(直到 2000 年代)。
You could just filter your data and call displot
over the filtered data:您可以过滤您的数据并在过滤后的数据上调用
displot
:
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.假设
data1
是一个 numpy 数组。
You can set a range for Axes
object that sns
returns.您可以为
sns
返回的Axes
对象设置范围。
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)}
:如果您希望仅针对 [0,10] 中的值计算 KDE 和直方图,您可以使用参数
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.确实如此,只需在声明绘图后将 plt.xlim(x,x1) 放在一行中,生成的绘图将只有 x 和 x1 之间的 x 值。 You can do the same for the y axis using ylim.
您可以使用 ylim 对 y 轴执行相同的操作。
In modern seaborn use the option binrange
of histplot
(note that distplot
is depreciated).在现代 seaborn 中,使用
binrange
的histplot
选项(注意distplot
已贬值)。
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.