绘制包含80%(x,y)点的圆(Plot circle that contains 80% (x, y) points)

编程入门 行业动态 更新时间:2024-10-28 02:21:12
绘制包含80%(x,y)点的圆(Plot circle that contains 80% (x, y) points)

我有一个二维数组(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:

enter image description here

更多推荐

本文发布于:2023-07-22 18:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222268.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Plot   points   circle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!