 # transformations

Hello list, I have just figured out how to use transformations

> for the 'Text' object from the class library to add a text to
> the graph (well actually this applies to any Artist object).

> If the coordinates are given as absolute _screen_ coordinates
> (very unlikely...), we use:

> t = Text(x=x, y=y, text=text)
> axes.texts.append(t)

> If however the coordinates are given as _data_ coordinates x,y
> then we use:

> t = Text(x=x, y=y, text=text)
> t.set_transform(axes.transData)
> ax.texts.append(t)

You can also pass the transform as a kwarg

t = Text(x=x, y=y, text=text, transform=axes.transData)
ax.texts.append(t)

> My question: Is there any transformation that would allow me to
> specify the coordinates as x,y tuple where (0,0) is bottom left
> and (1,1) is top right? In gnuplot this is known as the 'graph'
> coordinate system.

There are four built-in transforms that you should be aware of; below
ax is an Axes instance and fig is a Figure instance

matplotlib.transforms.identity_transform() # display coords
ax.transData # data coords
ax.transAxes # 0,0 is bottom,left of axes and 1,1 is top,right
fig.transFigure # 0,0 is bottom,left of figure and 1,1 is top,right

There is nothing wrong with instantiating Text instances yourself and
adding them to the figure or axes manually as you did, but both the
Axes and Figure have helper methods "text" which define a default
transformation that can be overridden

So

t = Text(x=x, y=y, text=text)
t.set_transform(axes.transAxes)
ax.texts.append(t)

is equivalent to

ax.text(x, y, text, transform=ax.transAxes)

The default transform for ax.text is ax.transData and the default
transform for fig.text is fig.transFigure.

Of course, you can define more general transformations, eg
matplotlib.transforms.Affine, but the four listed above arise in a lot
of applications.

If you have time and inclination to write a transformations tutorial
for the wiki, that would be great.

Thanks,
JDH

Hello John,

There is nothing wrong with instantiating Text instances yourself and
adding them to the figure or axes manually as you did, but both the
Axes and Figure have helper methods "text" which define a default
transformation that can be overridden

So

t = Text(x=x, y=y, text=text) t.set_transform(axes.transAxes)
ax.texts.append(t)

is equivalent to

ax.text(x, y, text, transform=ax.transAxes)

ax.text was my starting point to understand the usage of Text(). For my application I need a very detailed control of the objects I create, this is why I prefer the manual approach.

If you have time and inclination to write a transformations tutorial
for the wiki, that would be great.

Not tonight, but I will!