[I'm Cc:ing people since the mailing list forwarder seems to be
working really slowly.]
Hi Bill,
> You can even throw all the magic into one function like this:
>
> def const_offset(x,y):
[...]
>
> And then just add a transform=const_offset(x,y) parameter wherever
> you want one.
This will waste some memory by creating a new transform object every
time, which could matter if your plots contain lots of text labels.
You can simply create the transformation once and then use it when
needed.
Here's a better solution adapted from John Hunter's and Eric Firing's
posts and putting the transformation magic into a function:
#!/usr/bin/env python
import matplotlib
from matplotlib.transforms import blend_xy_sep_transform, \
identity_transform
from pylab import figure, show
def offset(ax, x, y):
# This makes a shallow copy of ax.transData:
# (as of svn 2630, there is copy_bbox_transform_shallow for this purpose)
trans = blend_xy_sep_transform(ax.transData, ax.transData)
trans.set_offset((x,y), identity_transform())
return trans
fig=figure()
ax=fig.add_subplot(111)
# plot some data
x = (3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3)
y = (2,7,1,8,2,8,1,8,2,8,4,5,9,0,4,5)
ax.plot(x,y,'.')
# add labels
trans=offset(ax, 10, 5)
for a,b in zip(x,y):
ax.text(a, b, '(%d,%d)'%(a,b), transform=trans)
show()
It has the advantage of working with logarithmic plots: try
setp(gca(), xscale='log'). It doesn't work with polar plots (and
neither does my previous suggestion).
Apparently trans.set_offset allows for just this kind of thing: adding
a pixel offset to an existing transformation. How about adding a
possibility to compose arbitrary transformations as functions? Then we
wouldn't have to worry about copying transData, and it would
automatically work for polar plots and whatever somebody will come up
with in the future, just by saying something like
trans = compose(ax.transData,
translation_transform(Value(10), Value(5)))
text(x, y, 'foobar', transformation=trans)
--
Jouni
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users