【问题标题】:How do I compute the barycenter of an histogram represented as polar coordinate如何计算表示为极坐标的直方图的重心
【发布时间】:2023-01-05 22:44:32
【问题描述】:

我将直方图显示为极坐标图,我想显示重心以查看直方图是否存在主导方向。 我的问题是当直方图有很多 0 和 2pi 值时,重心不起作用(因为 0 和 2pi 在极坐标图中是相同的角度)

所以我得到这样的东西:

我希望看到 0 度的红点。

这是一个最小的示例代码:

import numpy as np
import matplotlib.pyplot as plt

a = np.random.rand(1000)*3.14*2
a = np.hstack((a,np.ones(100)*0.1))
a = np.hstack((a,np.ones(100)*3.1*2))
f = plt.figure()
f.add_subplot(1,1,1,projection='polar')
n, bins = np.histogram(np.array(a), 50)
n = n / np.sum(n)
plt.hist((bins[0:-1] + bins[1:]) / 2, weights=n, bins=bins, rwidth=1.)
binscenter = (bins[0:-1] + bins[1:]) / 2
moyenne = np.sum(n * binscenter) / np.sum(n)
plt.scatter(moyenne ,0.1,c='r')

plt.show()

【问题讨论】:

    标签: python polar-coordinates


    【解决方案1】:

    你需要的是循环统计.

    我们通常使用具有线性支持的数据,即数据可以在实线上表示。但是像角度这样的量没有线性而是圆形的支持,所以我们需要专门的理论来处理它们。

    幸运的是,SciPy 已经有一些函数来计算循环数据的描述性统计。这是一个例子:

    import math
    
    import numpy as np
    import scipy
    
    data = [0.0, 0.0, 2 * math.pi, 2 * math.pi]
    
    linear_mean = np.mean(data)
    circular_mean = scipy.stats.circmean(data)
    

    如您所见,linear_mean返回pi,而circular_mean返回2pi

    如果你想了解更多关于循环统计的知识,我强烈建议阅读这篇book

    【讨论】:

      猜你喜欢
      • 2020-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-29
      • 1970-01-01
      • 2016-01-29
      • 2019-07-31
      • 2018-07-23
      相关资源
      最近更新 更多