英文:
Histogram() doesn't display the values at the edge of defined range
问题
我无法让Julia在定义了bins范围时显示直方图的边界值。以下是一个最小示例:
using Plots
x = [0, 0.5, 1]
plot(histogram(x, bins=range(0, 1, length=3)))
即使明确定义边界值也无济于事(bins=[0, 0.3, 0.7, 1]
)。似乎histogram()
排除了范围的限制。您可以扩展范围以使其正常工作:
plot(histogram(x, bins=[0, 0.3, 0.7, 1.01))
但我认为这不应该是解决方法。令人惊讶的是,固定bin的数量是有效的(nbins=3
),但我需要在不同运行之间保持所有bin的宽度相同以进行比较。
我尝试过Plots、PlotlyJS和StatsBase(使用fit()
和其closed
属性),但没有成功。也许我漏掉了什么,所以我想问一下:我想要的是否可能实现?
英文:
I can't get Julia to display edge values on histograms, when defining a range for the bins. Here is a minimal example:
using Plots
x = [0,0.5,1]
plot(histogram(x, bins=range(0,1,length=3)))
Defining them explicitly doesn't help (bins=[0,0.3,0.7,1]
). It seems that histogram()
excludes the limits of the range. I can extend the range to make it work:
plot(histogram(x, bins=[0,0.3,0.7,1.01))
But I really don't think that should be the way to go. Surprisingly, fixing the number of bins does work (nbins=3
) but I need to keep the width of all the bins the same and constant across different runs for comparison purposes.
I have tried with Plots, PlotlyJS and StatsBase (with fit()
and its closed
attribute) to no avail. Maybe I'm missing something, so I wanted to ask: is it possible to do what I want?
答案1
得分: 1
尝试:
plot(histogram(x, bins=range(0, nextfloat(1.0), length=3)))
尽管这扩展了范围,但只是以最小的方式这样做。基本上,这是使直方图的右端关闭的最小方式。
至于等宽度,当涉及到浮点数时,等宽度有不同的含义 - 以实数的方式(这些不总是可表示的),或者以值的数量为例,但对于 [0.0, 1.0] 和 [1.0, 2.0] 可能是不同的。
所以希望这满足了问题的需求。
英文:
Try:
plot(histogram(x, bins=range(0,nextfloat(1.0),length=3)))
Although this extends the range, it does so in a minimal way. Essentially the most minimal which turns the right end of the histogram closed.
As for equal widths, when dealing with floating points, equal widths has different meanings - in terms of real numbers (which are not always representible), or in terms (for example) of the number of values, but this can be different for [0.0,1.0] and [1.0,2.0].
So hopefully, this scratches the itch in the OP.
答案2
得分: -1
>closed:一个带有值:right或:left的符号,指示在哪一侧关闭箱子(半开区间或更高维度的类似物)。请参见下面的示例。
这在许多直方图实现中非常常见,例如Numpy
In [8]: np.histogram([0], bins=[0, 1, 2])
Out[8]: (array([1, 0]), array([0, 1, 2]))
In [9]: np.histogram([1], bins=[0, 1, 2])
Out[9]: (array([0, 1]), array([0, 1, 2]))
Numpy存在一个不一致性,即最后一个箱子两侧都关闭,但对于每个箱子的一侧关闭是完全正常的,
英文:
https://juliastats.org/StatsBase.jl/latest/empirical/#StatsBase.Histogram
most importantly:
>closed: A symbol with value :right or :left indicating on which side bins (half-open intervals or higher-dimensional analogues thereof) are closed. See below for an example.
this is very common in many histogram implementations, for example, Numpy
In [8]: np.histogram([0], bins=[0, 1, 2])
Out[8]: (array([1, 0]), array([0, 1, 2]))
In [9]: np.histogram([1], bins=[0, 1, 2])
Out[9]: (array([0, 1]), array([0, 1, 2]))
Numpy has the inconsistency that the last bin is closed on both sides, but it's perfectly normal for every bin to close on one side,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论