Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ploting filled polygons in python

I have two matrices Tri and V for faces (Nx3) and vertices (Mx3) of polygons that I want to plot. Is there any matplotlib (or any alternative) way to do that? Something similar to Matlab command

patch('faces',Tri,'vertices',V,'facecolor', 
      'flat','edgecolor','none','facealpha',1)
like image 390
ahmethungari Avatar asked Nov 14 '14 17:11

ahmethungari


People also ask

How do I fill a shape in Matplotlib?

pyplot. fill() function is used to fill the area enclosed by polygon /curve. sequence of x,y = To traverse the boundaries of the polygon or curve defined by lists of x and y positions of its nodes. color = To change the default fill color to the desired one.

How do you fill a pattern in Python?

Attention: since the backslash ( \ ) is a special character in Python, if we want to fill our bar plot with this pattern, we have to use a double backslash ( '\\' ). In this case, to obtain a denser pattern, it's necessary to assign an even numbers of backslashes to the hatch parameter ( '\\\\' , '\\\\\\' , etc.).


1 Answers

I'm not exactly sure what matlab does, but you can draw a polygon using matplotlib.patches.Polygon. Adapted from an example in the docs:

import numpy as np

import matplotlib.pyplot as plt
import matplotlib
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection

fig, ax = plt.subplots()
patches = []
num_polygons = 5
num_sides = 5

for i in range(num_polygons):
    polygon = Polygon(np.random.rand(num_sides ,2), True)
    patches.append(polygon)

p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)

colors = 100*np.random.rand(len(patches))
p.set_array(np.array(colors))

ax.add_collection(p)

plt.show()

enter image description here

like image 68
Hooked Avatar answered Oct 18 '22 00:10

Hooked