# Setting an artist transform

I am trying to animate a plot with a circle that will be

> moved around. c = Circle((0,0)) a = gca()
> a.add_artist(c) draw() # This works

> c.set_transform(matplotlib.transforms.translation_transform(0.1,
> 0.1)) draw() # SEGFAULT

> How do I update the transform?

Hey Charlie,

The segfault is occurring because you are using the
translation_transform incorrectly, though admittedly a more graceful
exit would be preferred. The translation_transform is expecting a
LazyValue instance

def translation_transform(tx, ty):
"""
return a pure tranlational transformation tx and ty are LazyValue
instances (Values or binary opertations on values)
"""

as described at http://matplotlib.sf.net/matplotlib.transforms.html

so rather than

trans = translation_transform(0.1, 0.1)

you want

trans = translation_transform(Value(0.1), Value(0.1))

But in debugging this, I also discovered a bug in the implementation
of translation_transform. Specifically, it should read

def translation_transform(tx, ty):
"""
return a pure tranlational transformation tx and ty are LazyValue
instances (Values or binary opertations on values)
"""
return Affine(one(), zero(), zero(), one(), tx, ty)

where one() has replaced zero()

Below is a complete script, which redefines the translation transform
(and thus works with regular matplotlib -- with CVS just use the
standard translation_transform). Note that with the lazy values of the
transform, you call the "set" method of the lazy value to update the
transform, eg,

trans = translation_transform(x,y)
c.set_transform(trans)
for i in range(0, 800, 5):
x.set(i)
y.set(i)

Complete example below....

JDH

from matplotlib.transforms import zero, one, Value, Affine
from matplotlib.patches import Circle
from pylab import figure, ion, show

def translation_transform(tx, ty):
"""
return a pure tranlational transformation tx and ty are LazyValue
instances (Values or binary opertations on values)
"""
return Affine(one(), zero(), zero(), one(), tx, ty)

ion()

fig = figure()

x = Value(0)
y = Value(0)
trans = translation_transform(x,y)
c.set_transform(trans)

for i in range(0, 800, 5):

x.set(i)
y.set(i)
if 0: # uncomment for extra info
verts = c.get_verts()
tverts = trans.seq_xy_tups(verts)
print i
print '\t', trans.as_vec6_val()
print '\t', verts
print '\t', tverts
ax.figure.canvas.draw()
show()