简体   繁体   中英

How to fill matplotlib polygon regardless of point order?

I am trying to make the area shown below all green.

So far I use this: But not the whole four-sided-figure is coloured.

x,y = [3.3333333333333335, 5.0, -0.0, -0.0], [3.333333333333333, 0.0, 5.0, 0.0]
my_plot.fill(x, y, "g")   

It would be possible to fix the rotation of x and y in this specific example. But in other times it would not work.

Is there a way to fill everything between the area between given points, regardless of any order of the points?

问题

SOLUTION:

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])

If you are sure that all the original points already lie on the convex hull, you can order them via the angle their coordinates make versus their center:

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)

查找并填充凸包

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM