绘制包含 80% (x, y) 点的圆
我有一个二维数组 (x, y) 点,我想绘制一个包含 80% 这些点的圆,并且我想知道结果圆的半径.有没有办法用python做到这一点?
I have a 2d-array (x, y) of points and I would like to plot a circle that contains the 80% of this points and also I would like to know the radius of the resulting circle. Is there any way to do this with python?
我一直在寻找一种方法,但没有成功.我很抱歉没有提出尝试,但我完全失去了在不给圆半径的情况下取 80% 的条件.
I've been looking for a way to do it but with no success. I apology for not presenting a try, but I'm totally lost with the condition of taking the 80% without giving a radius to the circle.
更新:
我尝试了以下方法:
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()
这是我想要获得的情节:
And this is the plot I want to obtain:
在这个例子中,我有 10 个点,圆是里面有 8 个点的最小圆.我已经通过眼睛完成了这个示例,但我想要的是:给定 x 和 y,获取与至少 80% 的点在其中的条件相匹配的圆的参数(中心位置和半径),即,how 必须是包含至少 80% 点的最小圆.这可能吗?
In this example I have 10 points and the circle is the minimum circle where 8 points are inside. I have done this example by eye, but what I want is: given x and y, get the parameters of the circle (the center position and the radius) that match the condition that at least 80% of points are inside it, i.e., how has to be the minimum circle that contains at least 80% of points. Is this possible?
正如其他人所说,您可以绘制许多不同的圆圈以获得所需的结果.但是,一种快速而肮脏的方法可能是:
As said by others, you could draw many different circles to obtain the requested result. But one quick and dirty way to do this might be:
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()
结果: