获取垃圾箱直方图python的中心

发布于 2025-02-09 12:46:24 字数 286 浏览 2 评论 0原文

我正在使用 > matplotlib.pyplot的功能,给出一些坐标(x,y)

我想在定义直方图后,以获取每个垃圾箱的中心,即每个垃圾箱的中心的坐标。

有一种简单的方法来获取它们吗?

I'm using the hist2d function of matplotlib.pyplot, giving it input some coordinates (x,y).

I would like, after having defined the histogram, to get the center of each bin, i.e., the coordinates of the center of each bin.

Is there an easy way to get them?

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

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

发布评论

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

评论(1

℉絮湮 2025-02-16 12:46:24

plt.plt.hist.hist >包括垃圾箱边缘,因此请按左边缘和右边缘的平均值:

​如果首选,您可以使用Numpy函数,尽管在这种情况下我发现它不太可读:

xcenters = np.mean(np.vstack([xedges[:-1], xedges[1:]]), axis=0)

plt.hist2d

import matplotlib.pyplot as plt
import numpy as np

x = np.random.random(50)
y = np.random.random(50)

h, xedges, yedges, image = plt.hist2d(x, y, bins=5)

xcenters = (xedges[:-1] + xedges[1:]) / 2
ycenters = (yedges[:-1] + yedges[1:]) / 2

输出:

>>> xedges
# array([0.01568168, 0.21003078, 0.40437988, 0.59872898, 0.79307808, 0.98742718])

>>> xcenters
# array([0.11285623, 0.30720533, 0.50155443, 0.69590353, 0.89025263])

>>> yedges
# array([0.00800735, 0.20230702, 0.39660669, 0.59090636, 0.78520603, 0.97950570])

>>> ycenters
# array([0.10515718, 0.29945685, 0.49375652, 0.68805619, 0.88235586])

The return values of plt.hist and plt.hist2d include bin edges, so take the mean of the left edges and right edges:

  • plt.hist

    h, xedges, patches = plt.hist(x)
    xcenters = (xedges[:-1] + xedges[1:]) / 2
    
  • plt.hist2d

    h, xedges, yedges, image = plt.hist2d(x, y)
    xcenters = (xedges[:-1] + xedges[1:]) / 2
    ycenters = (yedges[:-1] + yedges[1:]) / 2
    

Note that you can use numpy functions if preferred, though I find it less readable in this case:

xcenters = np.mean(np.vstack([xedges[:-1], xedges[1:]]), axis=0)

Full example with plt.hist2d:

import matplotlib.pyplot as plt
import numpy as np

x = np.random.random(50)
y = np.random.random(50)

h, xedges, yedges, image = plt.hist2d(x, y, bins=5)

xcenters = (xedges[:-1] + xedges[1:]) / 2
ycenters = (yedges[:-1] + yedges[1:]) / 2

Output:

>>> xedges
# array([0.01568168, 0.21003078, 0.40437988, 0.59872898, 0.79307808, 0.98742718])

>>> xcenters
# array([0.11285623, 0.30720533, 0.50155443, 0.69590353, 0.89025263])

>>> yedges
# array([0.00800735, 0.20230702, 0.39660669, 0.59090636, 0.78520603, 0.97950570])

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