I would like to calculate the size (height and width) of a text label in matplotlib in data coordinates so I can move it in a certain direction by its own size. Ideally I would like to know the size before I draw it.
import matplotlib.pylab as pylab
pylab.figure()
x, y = 5, 7
text = 'label'
pylab.plot(x, y, 'k.')
pylab.text(x, y, text, color='red')
pylab.show()
In the above example the label appears over the point, I'd like to move it in a direction by it's size in data coordinates.
EDIT:
Attempted:
import matplotlib.pylab as pylab
fig = pylab.figure()
x, y = 5, 7
text = 'label'
pylab.plot(x, y, 'k.')
t = pylab.text(x, y, text, color='red')
fig.canvas.draw()
bbox = t.get_window_extent(renderer = pylab.gcf().canvas.get_renderer())
pylab.plot([bbox.x0, bbox.x1, bbox.x1, bbox.x0, bbox.x0], [bbox.y0, bbox.y0, bbox.y1, bbox.y1, bbox.y0], 'r-')
pylab.show()
If I use:
pylab.plot([bbox.x0, bbox.x1, bbox.x1, bbox.x0, bbox.x0], [bbox.y0, bbox.y0, bbox.y1, bbox.y1, bbox.y0], 'g-', transform=None)
I don't see the bounding box
from Can you calculate the size of a text annotation in matplotlib?


No comments:
Post a Comment