19

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)
smci
  • 32,567
  • 20
  • 113
  • 146
ahmethungari
  • 2,089
  • 4
  • 19
  • 21
  • You probably want to convert the polygons to triangles, and then use [`plot_tri_surf`](http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#tri-surface-plots). – will Nov 14 '14 at 17:57

1 Answers1

39

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

TastySlowCooker
  • 135
  • 1
  • 6
Hooked
  • 84,485
  • 43
  • 192
  • 261