【发布时间】:2016-08-30 02:32:03
【问题描述】:
我使用 matplotlib 1.15.1 并尝试生成这样的散点图:
椭圆有固定大小,并用中心坐标、宽度、高度和角度绘制(从外部提供):我不知道它们的引号是什么。
g_ell_center = (0.8882, 0.8882)
g_ell_width = 0.36401857095483
g_ell_height = 0.16928136341606
g_ellipse = patches.Ellipse(g_ell_center, g_ell_width, g_ell_height, angle=angle, fill=False, edgecolor='green', linewidth=2)
这个省略号应该在我的绘图上标记正常和半正常数据。 然后,我有一个大约 500 个点的数组,必须根据它们所属的椭圆着色。所以我尝试用 contains_point 方法检查每个点:
colors_array = []
colors_scheme = ['green', 'yellow', 'black']
for point in points_array:
if g_ellipse.contains_point(point, radius=0):
colors_array.append(0)
elif y_ellipse.contains_point(point, radius=0):
colors_array.append(1)
else:
colors_array.append(2)
最后绘制点:
plt.scatter(x_array, y_array, s=10, c=[colors_scheme[x] for x in colors_array], edgecolor="k", linewidths=0.3)
但是 contains_point 非常慢!它为 300 点散点图工作了 5 分钟,我必须并行生成数千个。也许有更快的方法? 附言整个项目绑定了matplotlib,其他库我用不了。
【问题讨论】:
-
确定椭圆的两个焦点,计算点到两个焦点的距离之和。如果这小于主轴,则该点在椭圆内。 en.wikipedia.org/wiki/Ellipse
标签: python python-3.x matplotlib ellipse