Matplotlib a une interface Path
qui fonctionne très similaire: http://matplotlib.org/users/path_tutorial.html
Mais vous devrez charger le texte et faire un peu de vous-même munging. Mais considérez cet exemple:
import matplotlib.path as mpath
import matplotlib.patches as patches
import matplotlib.pyplot as plt
Commencez avec le texte, en tant que chaîne, à partir de votre exemple.
instr = """newpath
0 0 moveto
1 0 lineto
1 1 lineto
0 1 lineto
0 0 lineto
stroke"""
Séparez les sauts de ligne et supprimez la première et la dernière ligne. Peut-être que le dernier devrait être utilisé pour le formatage final?
verts = instr.split('\n')[1:-1]
verts
Convertissez chaque ligne en coordonnées et en codes.
code_lookup = {'moveto': mpath.Path.MOVETO,
'lineto': mpath.Path.LINETO}
coords = [(i.split(' ')[0], i.split(' ')[1]) for i in verts]
codes = [code_lookup[i.split(' ')[2]] for i in verts]
# set the last code to close the polygon
codes[-1] = mpath.Path.CLOSEPOLY
Construire le polygone.
mypath = mpath.Path(coords, codes)
mypatch = patches.PathPatch(mypath, facecolor='orange', lw=2)
Trace le résultat.
fig, ax = plt.subplots(subplot_kw={'aspect': 1.0,
'xlim': [-1,2],
'ylim': [-1,2]})
ax.add_patch(mypatch)
Si vous voulez juste montrer les chiffres PostScript dans IPython, vous pourriez envisager PostCanvas: http://www.feiri.de/pcan/ – Alan