# Endpoint of a text string in a plot?

Hi

How do I get the information about the endpoint of a text element?

Example:

import matplotlib.pylab as plt
text = plt.text(0.5,0.5,'hi')
plt.show()

In the dataTrans coordinates, where does the text string ends?

I have spent some time now trying to solve this, seemingly simple problem,
so I appreciate any help a lot!

/ Keld

···

View this message in context: http://old.nabble.com/Endpoint-of-a-text-string-in-a-plot--tp32977548p32977548.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

It is actually a very difficult thing to do, and is why mpl does not provide a mechanism to find out until after rendering. The problem is that – in the general case – it is not possible to know ahead of time how much space some text will take up for an arbitrary font, style and size.

You can have the figure get rendered, and then you can query the text’s bounding box, if I understand correctly. Maybe the code for tight_layout() might show how it does this.

Ben Root

···

On Wednesday, December 14, 2011, Keld Lundgaard <keld.lundgaard@…287…> wrote:

Hi

How do I get the information about the endpoint of a text element?

Example:

import matplotlib.pylab as plt
text = plt.text(0.5,0.5,‘hi’)
plt.show()

In the dataTrans coordinates, where does the text string ends?

I have spent some time now trying to solve this, seemingly simple problem,
so I appreciate any help a lot!

/ Keld

Thank you Benjamin.

Yes, I understand the problem.

Thank you for the help. I am able to use Annotate for my specific problem: I
want to have a line starting from the end of the text to make the tick
labels with a left alignment.
However, a feature to get the boundary box would be useful for this, to
ensure that the plot is tight.

Best regards

Keld

Benjamin Root-2 wrote:

···

On Wednesday, December 14, 2011, Keld Lundgaard <keld.lundgaard@...287...> > wrote:

Hi

How do I get the information about the endpoint of a text element?

Example:

import matplotlib.pylab as plt
text = plt.text(0.5,0.5,'hi')
plt.show()

In the dataTrans coordinates, where does the text string ends?

I have spent some time now trying to solve this, seemingly simple
problem,
so I appreciate any help a lot!

/ Keld

It is actually a very difficult thing to do, and is why mpl does not
provide a mechanism to find out until after rendering. The problem is
that
-- in the general case -- it is not possible to know ahead of time how
much
space some text will take up for an arbitrary font, style and size.

You can have the figure get rendered, and then you can query the text's
bounding box, if I understand correctly. Maybe the code for
tight_layout()
might show how it does this.

Ben Root

------------------------------------------------------------------------------
10 Tips for Better Server Consolidation
Server virtualization is being driven by many needs.
But none more important than the need to reduce IT complexity