在点周围绘制椭圆 [英] Draw ellipses around points

查看:163
本文介绍了在点周围绘制椭圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用matplotlib在图上的一组点上绘制椭圆。我想获得这样的内容:





一个组的数据集(例如红色)可能如下所示:

  [[ -  23.88315146 -3.26328266]#第一点
[-25.94906669 -1.47440904]#第二点
[-26.52423229 -4.84947907]]#第三点

我可以很容易地在图上绘制点,但我遇到了绘制椭圆的问题。



椭圆的直径为 2 *标准偏差,其中心坐标为(x_mean,y_mean) 。一个椭圆的宽度等于 x标准偏差* 2 。它的高度等于 y标准差* 2



然而,我不知道如何计算角度(你可以在图片上看到椭圆不是完美的垂直线)。



你有关于如何去做的想法吗?



注意:
这个问题是LDA问题(线性判别分析)的简化。我试图将问题简化为最基本的表达式。 解决方案

这与数学有很多关系,而不是编程;)



既然你已经有了尺寸并且只想找到角度,下面是我要做的(基于我的直觉):



尝试找到最适合给定的一组点(趋势线)的线,这也称为线性回归。有几种方法可以做到这一点,但 Least Squares 方法是一个相对容易的方法(见下文)。



一旦找到最佳拟合线,你可以使用斜率作为你的角度。



最小二乘线性回归



最小二乘线性回归方法用于查找趋势线的斜率,正是我们想要的。



以下视频解释了它的工作原理



假设您有一个数据集: data = [(x1,y1),(x2,y2),.. 。]



使用最小二乘法,您的斜率为:

 #我在你的例子中看到你已经有了x_mean和y_mean 
#无需再次计算它们,跳过下面两行
#并在示例的其余部分中使用您的值
avg_x = sum(元素[0]代表数据中的元素)/ len(数据)
avg_y = sum(数据元素的元素[1]) / len(data)

x_diff = [元素[0] - 数据元素的avg_x]
y_diff = [元素[1] - 数据元素的avg_y]

x_diff_squared = [元素** 2 for x_diff中的元素]

slope = sum(x * y代表zip中的x,y(x_diff,y_diff))/ sum(x_diff_squared)

一旦你有了,你就会大部分完成。斜率等于角度的切线 slope = tan(角度)



使用python的 math module angle = math.atan(slope)这将返回弧度的角度。如果你想要度数,你必须使用 math.degrees(angle)



来转换它。尺寸和位置你已经有了,你得到了一个椭圆;)




这就是我将如何解决这个问题,但是可能有一千种不同的方法可能会起到
的作用,并且最终可能会比我提出的更好(也更复杂)。


I'm trying to draw ellipses around points of a group on a graph, with matplotlib. I would like to obtain something like this:

A dataset for a group (the red one for example) could look like this:

[[-23.88315146  -3.26328266]  # first point
 [-25.94906669  -1.47440904]  # second point
 [-26.52423229  -4.84947907]]  # third point

I can easily draw the points on a graph, but I encounter problems to draw the ellipses.

The ellipses have diameters of 2 * standard deviation, and its center has the coordinates (x_mean, y_mean). The width of one ellipse equals the x standard deviation * 2. Its height equals the y standard deviation * 2.

However, I don't know how to calculate the angle of the ellipses (you can see on the picture the ellipses are not perfectly vertical).

Do you have an idea about how to do that ?

Note: This question is a simplification of LDA problem (Linear Discriminant Analysis). I'm trying to simplify the problem to its most basic expression.

解决方案

This has a lot more to do with mathematics than programming ;)

Since you already have the dimensions and only want to find the angle, here is what I would do (based on my instinct):

Try to find the line that best fits the given set of points (trendline), this is also called Linear Regression. There are several methods to do this but the Least Squares method is a relatively easy one (see below).

Once you found the best fitting line, you could use the slope as your angle.

Least Squares Linear Regression

The least squares linear regression method is used to find the slope of the trendline, exactly what we want.

Here is a video explaining how it works

Let's assume you have a data set: data = [(x1, y1), (x2, y2), ...]

Using the least square method, your slope would be:

# I see in your example that you already have x_mean and y_mean
# No need to calculate them again, skip the two following lines
# and use your values in the rest of the example
avg_x = sum(element[0] for element in data)/len(data)
avg_y = sum(element[1] for element in data)/len(data)

x_diff = [element[0] - avg_x for element in data]
y_diff = [element[1] - avg_y for element in data]

x_diff_squared = [element**2 for element in x_diff]

slope = sum(x * y for x,y in zip(x_diff, y_diff)) / sum(x_diff_squared)

Once you have that, you are almost done. The slope is equal to the tangent of the angle slope = tan(angle)

Use python's math module angle = math.atan(slope) this will return the angle in radians. If you want it in degrees you have to convert it using math.degrees(angle)

Combine this with the dimensions and position you already have and you got yourself an ellipse ;)


This is how I would solve this particular problem, but there are probably a thousand different methods that would have worked too and may eventually be better (and more complex) than what I propose.

这篇关于在点周围绘制椭圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆