Skip to content

Instantly share code, notes, and snippets.

@automata
Created November 28, 2012 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save automata/4164025 to your computer and use it in GitHub Desktop.
Save automata/4164025 to your computer and use it in GitHub Desktop.
question about matplotlib
# -*- coding: utf-8 -*-
import numpy as n
import random as r
import pylab as p
DIRS = ['up', 'right', 'down', 'left']
def new_guy():
dna = [[r.choice(DIRS),
r.randint(1, 40),
1] for i in xrange(20)]
return dna
def coords_guy(guy):
x = 0
y = 0
xs = [0]
ys = [0]
for instr in guy:
if instr[0] is 'up':
y += instr[1] * instr[2]
elif instr[0] is 'down':
y -= instr[1] * instr[2]
elif instr[0] is 'right':
x += instr[1] * instr[2]
elif instr[0] is 'left':
print 'hey'
x -= instr[1] * instr[2]
xs.append(x)
ys.append(y)
return (xs, ys)
def guy_to_fig(guy):
xs, ys = coords_guy(guy)
p.clf()
p.axis('off')
p.plot(xs, ys, 'k-')
p.savefig('guy.png')
guy_to_fig(new_guy())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment