What I want to do:
I want to get the position and dimensions of a text instance in matplotlib world units (not screen pixels), with the intention of calculating and preventing text overlaps.
I'm developing on Mac OSX 10.9.3, Python 2.7.5, matplotlib 1.3.1.
What I've tried:
Let t be a text instance.
t.get_window_extent(renderer):
- This gets bounding box dimensions in pixels, and I need world coordinates (normalized between -1.0 and 1.0 in my case).
t._get_bbox_patch():
t = ax.text(x, y, text_string, prop_dict, bbox=dict(facecolor='red', alpha=0.5, boxstyle='square')) print t._get_bbox_patch()
- When I execute the above sequence, the output is
FancyBboxPatchFancyBboxPatch(0,0;1x1)
. In the image I produce, the text instance is rendered properly with a red bounding box, so that output leads me to think that the FancyBbox is instantiated but not actually populated with real dimensions until render time.
So, how can I get the position and dimensions of the text instance's bounding box in the same coord system units that I used for the x and y parameters I passed to ax.text(...)
?
Thanks for any tips!
This may help a bit.
import matplotlib.pyplot as plt f = plt.figure() ax = f.add_subplot(111) ax.plot([0,10], [4,0]) t = ax.text(3.2, 2.1, "testing...") # get the inverse of the transformation from data coordinates to pixels transf = ax.transData.inverted() bb = t.get_window_extent(renderer = f.canvas.renderer) bb_datacoords = bb.transformed(transf) # Bbox('array([[ 3.2 , 2.1 ],\n [ 4.21607125, 2.23034396]])')
This should give what you want. If you want to have the coordinates in terms of figure coordinates (0..1,0..1), then use the inverse of ax.transAxes
.
However, there is a small catch in this solution. An excerpt from the matplotlib
documentation:
Any Text instance can report its extent in window coordinates (a negative x coordinate is outside the window), but there is a rub.
The RendererBase instance, which is used to calculate the text size, is not known until the figure is drawn (draw()). After the window is drawn and the text instance knows its renderer, you can call get_window_extent().
So, before the figure is really drawn, there seems to be no way to find out the text size.
BTW, you may have noticed that the Bbox
instances have method overlaps
which may be used to find out whether the Bbox
overlaps with another one (bb1.overlaps(bb2)
). This may be useful in some cases, but it does not answer the question "how much".
If you have rotated texts, you will have hard time seeing if they overlap, but that you probably already know.