我有一个点的二维数组 (x, y),我想绘制一个包含 80% 点的圆,我还想知道所得圆的半径。有什么办法可以用 python 做到这一点吗?
我一直在寻找一种方法来做到这一点,但没有成功。我为没有尝试而道歉,但我完全迷失了在不给圆半径的情况下获得 80% 的条件。
更新:
我试过以下方法:
import matplotlib.pyplot as plt
x=[1, 1.15, 1.23, 0.92, 1.31, 1.18, 1.27, 1.07, 3, 3.2]
y=[1.17, 0.95, 1.04, 1.32, 1, 1.22, 1.28, 0.99, 1, 1.2]
plt.plot(x, y, 'bo')
circle=plt.Circle((1.1, 1.12), 0.2, color='g', fill=False)
fig = plt.gcf()
fig.gca().add_artist(circle)
plt.axis([0, 3.5, 0, 3.5])
plt.show()
这是我想要获得的情节:
在这个例子中,我有 10 个点,圆是包含 8 个点的最小圆。我已经通过眼睛完成了这个例子,但我想要的是:给定 x 和 y,得到圆的参数(圆心位置和半径)匹配至少 80% 的点在圆内的条件,即,如何必须是包含至少 80% 点的最小圆。这可能吗?
请您参考如下方法:
正如其他人所说,您可以绘制许多不同的圆来获得请求的结果。但一种快速而肮脏的方法可能是:
import numpy as np
import matplotlib.pyplot as plt
# generate some random points
n = 1000
x = 4 * np.random.randn(n) + 15
y = 2 * np.random.randn(n) + 10
# somehow compute center of cloud, use e.g. medium or mean
x0 = np.median(x)
y0 = np.median(y)
# compute radius
r = np.sqrt((x - x0)**2 + (y - y0)**2)
t = 80 # percent
r0 = np.percentile(r, t)
n_within = (r < r0).sum()
# make plot
plt.plot(x, y, '.')
circle = plt.Circle((x0, y0), r0, color='r', fill=False)
plt.gca().add_artist(circle)
plt.title('Found center at ({:.2f}, {:.2f})\n'
'{}% radius is {:.2f}\n'
'{} / {} points within circle'.format(
x0, y0, t, r0, n_within, n))
plt.axis([0, 30, 0, 20])
plt.show()
结果:
