簡體   English   中英

無論點順序如何,如何填充 matplotlib 多邊形?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM