我有一个二维数组(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%点的最小圆。 这可能吗?
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?
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.
Update:
I've tried the following:
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:
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?
最满意答案
正如其他人所说,你可以画很多不同的圈子来获得要求的结果。 但是,一个快速和肮脏的方式来做到这一点可能是:
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()结果:
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()Result:
更多推荐
发布评论