Matplotlib 直方图箱的选择取决于数据是否“单独”绘制。或其他一些数据:怎么会这样?

发布于 2024-12-21 08:30:33 字数 438 浏览 2 评论 0原文

标题就有了。 :)

示例:

from numpy import random
from matplotlib import pyplot as plt

data = [1 + random.randn(1000), random.randn(1000)]
bins = 10

plt.hist(data, bins, label=['first', 'second'])
plt.hist(data[1], bins, histtype='step', label=['second again'])

plt.legend()

plt.show()

给出(选择“步骤”类型以帮助“可见性”,与默认值相同):

该死的东西; )

看到了吗?

The title has it. :)

Example:

from numpy import random
from matplotlib import pyplot as plt

data = [1 + random.randn(1000), random.randn(1000)]
bins = 10

plt.hist(data, bins, label=['first', 'second'])
plt.hist(data[1], bins, histtype='step', label=['second again'])

plt.legend()

plt.show()

gives ('step' type chosen to aid "viewability", its the same with defaults):

the damn thing ;)

See?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清浅ˋ旧时光 2024-12-28 08:30:33

以下是 hist 当前使用(在 matplotlib git 存储库中)确定 bin 的过程:

  1. 如果给出 bins 作为实际 bin,则将使用这些 bin
  2. 如果给出 bin_range,则将使用这两个值作为 bin_range。
  3. 如果未给出 bin_range,则使用 [min of all data, max of all data] 作为 bin_range。
  4. 对每组数据使用 numpy 的直方图函数,参数为 bin_range 和 bins。然而,垃圾箱被直方图的结果所取代。这意味着,通过在第一组数据上使用 bins = 10 和 bin_range = [min of all data, max of all data] 调用 numpy 的直方图,最终在两组输入数据的第一个示例中确定 bin。

正如您可以想象的那样,您将获得一组不同的 bin 来将 hist 与一个数据集、另一个数据集以及这两个数据集与这些条件一起使用,这并不奇怪,因为 bin_range 在所有三个实例中可能都不同。

Here is the process that hist currently uses (in the matplotlib git repo) for determining the bins:

  1. If bins is given as the actual bins, those bins will be used
  2. If bin_range is given, then those two values will be used for the bin_range.
  3. If bin_range is not given, then use [min of all data, max of all data] as the bin_range.
  4. Use numpy's histogram function on each set of data with the parameters bin_range and bins. However bins is replaced with what comes out of histogram. That means that the bins are ultimately determined in your first example of two sets of input data by calling numpy's histogram with bins = 10 and bin_range = [min of all data, max of all data] on the first set of data.

As you can imagine, it isn't surprising that you will get a different set of bins for using hist with one data set, the other data set, and both together with these criteria because the bin_range will likely be different in all three instances.

我不在是我 2024-12-28 08:30:33

好吧,没有人说不同的 x 轴值间隔的 bin 是相同的;)

就是这样(请参阅已接受的答案):

from numpy import random
from matplotlib import pyplot as plt


data = [1 + random.randn(1000), random.randn(1000)]
num_bins = 10

_n, bins, _patches = plt.hist(data, num_bins, label=['first', 'second'])
plt.hist(data[1], bins, histtype='step', label=['second again'])

plt.legend()

plt.show()

给出:

在此处输入图像描述

Ok, no one said the bins will be the same for different x-axis value intervals ;)

Here it is (see the accepted answer):

from numpy import random
from matplotlib import pyplot as plt


data = [1 + random.randn(1000), random.randn(1000)]
num_bins = 10

_n, bins, _patches = plt.hist(data, num_bins, label=['first', 'second'])
plt.hist(data[1], bins, histtype='step', label=['second again'])

plt.legend()

plt.show()

giving:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文