Blog Archive

Saturday, September 8, 2012

How to Draw in Python

import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches

verts = [
    (0., 0.), 
    (0., 4.), 
    (1., 1.), 
    (4., 0.), 
    (0., 0.), 
    ]

codes = [Path.MOVETO,
         Path.LINETO,
         Path.LINETO,
         Path.LINETO,
         Path.CLOSEPOLY,
         ]

sqpath = Path(verts, codes)
plt.clf()
fig = plt.figure()
ax = fig.add_subplot(111)
sqpatch = patches.PathPatch(sqpath, facecolor='green', lw=2)
ax.add_patch(sqpatch)
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)
plt.show()

this will output the figure below:

No comments:

Post a Comment