[英]How to fill matplotlib polygon regardless of point order?
我正在尝试使下面显示的区域全部变为绿色。
到目前为止,我使用这个:但并不是整个四边形都是彩色的。
x,y = [3.3333333333333335, 5.0, -0.0, -0.0], [3.333333333333333, 0.0, 5.0, 0.0]
my_plot.fill(x, y, "g")
在这个特定示例中,可以修复 x 和 y 的旋转。 但在其他时候,它就行不通了。
有没有办法填充给定点之间区域之间的所有内容,而不管点的任何顺序?
解决方案:
from scipy.spatial import ConvexHull
points = np.array(Utils2D.get_valid_intersections(lin_prog.constraints))
hull = ConvexHull(points)
my_plot.fill(points[hull.vertices, 0], points[hull.vertices, 1])
如果您确定所有原始点都已经位于凸包上,您可以通过它们的坐标相对于它们的中心的角度对它们进行排序:
import matplotlib.pyplot as plt
import numpy as np
x, y = np.array([3.3333333333333335, 5.0, -0.0, -0.0]), np.array([3.333333333333333, 0.0, 5.0, 0.0])
order = np.argsort(np.arctan2(y - y.mean(), x - x.mean()))
plt.fill(x[order], y[order], "g", alpha=0.5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.