Is there any Python function (matplotlib or mayavi) that can execute the same task as "fill" in Matlab? What I need is, given a polygon defined by a set of points x, y and a color vector c for each point, fill(x, y, c)
would plot the polygon defined by (x, y) with a color in c[i] for each (x[i], y[i]).
matplotlib
does it less straight forward than matlab
, you need to add polygon
to the axes.
from matplotlib.patches import Polygon
fig, ax = plt.subplots()
N = 5
polygon = Polygon(np.random.rand(N, 2), True, facecolor='r')
ax.add_patch(polygon)
Note: facecolor
controls the color of the polygon, and accept string, RGBA or html color code as value.
if you have a set of Polygon and each of them need to be of a different color you can use the pathcollection:
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
fig, ax = plt.subplots()
N = 5
val = np.random.rand(N, 2, 3)
patches = [Polygon(val[:, :, i], True) for i in range(val.shape[-1])]
p = PatchCollection(patches, alpha=0.4)
p.set_array(np.random.rand(3)) # assign colors
ax.add_collection(p)
fig.colorbar(p)