【发布时间】: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()
【问题讨论】: