2015-11-17 130 views
1

我试图在我的情节中以这种方式绘制一个椭圆来表示一个集群。这里下面的代码绘制第一张图片。这是在for循环内,但它并不重要基于matplotlib的数据绘制椭圆

native_f1 = [some values here] 
native_f2 = [some values here] 

ax = plt.subplot(2, 2, index) 
ax.scatter(native_f1, native_f2, s=40) 
axes = plt.gca() 
axes.set_xlim([min(native_f1) - 800, max(native_f1) + 800]) 
axes.set_ylim([min(native_f2) - 800, max(native_f2) + 800]) 

no ellipses

现在我想获得类似的东西:

enter image description here

如何绘制一个椭圆基于我用来绘制这些图表的值? 谢谢

回答

1

ellipse的等式为$ \ pm b \ sqrt {1-(x-x0)^ 2/a^2} + y0 $,其中(x0,y0)是椭圆的中心。 (有关更多详细信息,请参阅Draw ellipses around points)。

您可以通过x2 = max(native_f1),x1 = min(native_f1),y2 = max(native_f2)y1 = min(native_f2)找到椭圆的边缘。中心(x0,y0)将为((x2+x1)/2, (y2+y1)/2)。 y方向(b)的轴的比例尺将为(y2-y1)/2,并且x方向(a)的轴的比例尺将为(x2-x1)/2。必要时用调整因子进行调整。你可以使用matplotlib.patches.Ellipse

+0

工作就像一个魅力!谢谢 – davideberdin