"Bill Baxter" <[EMAIL PROTECTED]> writes:

> I want to draw some labels with plot.text() and have them appear a
> given number of pixels (or mm, or points) to above and to the right of
> the data points they are describing.   Is there some way to specify a
> screen offset from a point in graph coordinates?  

Here's what I came up with. Perhaps the matplotlib gurus know a
simpler way... (One way to simplify this would be to add a new
"offset" Func in _transforms.cxx, so the user could just copy the
transData transform and call set_funcx and set_funcy with suitable
offset functions.)

#!/usr/bin/env python

import matplotlib
from matplotlib.transforms import Value, zero, Affine, Point
from pylab import figure, show

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)

fig=figure()
ax=fig.add_subplot(111)
ax.plot(x,y,'.')

offset = Point(Value(10), Value(5))

ll1 = ax.transData.get_bbox1().ll()
ur1 = ax.transData.get_bbox1().ur()
ll2 = ax.transData.get_bbox2().ll()
ur2 = ax.transData.get_bbox2().ur()

scale_x = (ur2.x()-ll2.x())/(ur1.x()-ll1.x())
scale_y = (ur2.y()-ll2.y())/(ur1.y()-ll1.y())
trans = Affine(scale_x, zero(), zero(), scale_y,
               ll2.x()-scale_x*ll1.x() + offset.x(),
               ll2.y()-scale_y*ll1.y() + offset.y())

for a,b in zip(x,y):
    ax.text(a, b, '(%d,%d)'%(a,b), transform=trans)

show()
If you run this and then pan and zoom, the offsets of the text labels
should stay constant.

-- 
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

Reply via email to