trailing space in text string stripped, making it impossible to right-pad my text

Hello. My problem is as follows:
(ipython --pylab)

from pylab import *
pp=plot([0,0],[1,1])
text(xlim()[0],1,' Need padding ',horizontalalignment='left')
text(xlim()[1],1,' Need padding ',horizontalalignment='right')

The second case does not do what I want, which is to pad the text on the
right. Text strings are stripped on the right, but no on the left. How can I
elegantly create a character of space?

Thanks!
c

···

--
View this message in context: http://www.nabble.com/trailing-space-in-text-string-stripped%2C-making-it-impossible-to-right-pad-my-text-tp25639703p25639703.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Christopher Barrington-Leigh wrote:

Hello. My problem is as follows:
(ipython --pylab)

from pylab import *
pp=plot([0,0],[1,1])
text(xlim()[0],1,' Need padding ',horizontalalignment='left')
text(xlim()[1],1,' Need padding ',horizontalalignment='right')

The second case does not do what I want, which is to pad the text on the
right. Text strings are stripped on the right, but no on the left. How can
I elegantly create a character of space?

Thanks!
c

Untested, but I think you could do this with just python builtin types:

from pylab import *
pp=plot([0,0],[1,1])
text(xlim()[0],1,'string'.ljust(10,' '))
text(xlim()[1],1,'string'.rjust(10,' '))

See the documention:
http://docs.python.org/library/stdtypes.html

str.ljust(width[, fillchar])¶

Return the string left justified in a string of length width. Padding is
done using the specified fillchar (default is a space). The original string
is returned if width is less than len(s).

Changed in version 2.4: Support for the fillchar argument.

···

--
View this message in context: http://www.nabble.com/trailing-space-in-text-string-stripped%2C-making-it-impossible-to-right-pad-my-text-tp25639703p25653102.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

I can confirm this with latest svn. I take a quick look at the code,
but couldn't figure where the stripping happens and I hope other
experts to step in.

While I think you're not using usetex mode, you may use tex's own
spacing command with usetex mode. Depending on your need, you may also
use the annotate function with which you can provide a spacing in
pixel coordinate (or other coordinate the annotate supports).

Regards,

-JJ

···

On Sun, Sep 27, 2009 at 9:57 PM, Christopher Barrington-Leigh <cpblpublic+nabble@...287...> wrote:

Hello. My problem is as follows:
(ipython --pylab)

from pylab import *
pp=plot([0,0],[1,1])
text(xlim()[0],1,' Need padding ',horizontalalignment='left')
text(xlim()[1],1,' Need padding ',horizontalalignment='right')

The second case does not do what I want, which is to pad the text on the
right. Text strings are stripped on the right, but no on the left. How can I
elegantly create a character of space?

Thanks!
c
--
View this message in context: http://www.nabble.com/trailing-space-in-text-string-stripped%2C-making-it-impossible-to-right-pad-my-text-tp25639703p25639703.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Thanks. Unfortunately this gives me the same results: no good. Something is
stripping whatever filler is there on the right.

text(xlim()[1],1.01,'string'.ljust(10,' '),horizontalalignment='right')

John [H2O] wrote:

···

Untested, but I think you could do this with just python builtin types:

from pylab import *
pp=plot([0,0],[1,1])
text(xlim()[0],1,'string'.ljust(10,' '))
text(xlim()[1],1,'string'.rjust(10,' '))

See the documention:
http://docs.python.org/library/stdtypes.html

str.ljust(width[, fillchar])¶

Return the string left justified in a string of length width. Padding is
done using the specified fillchar (default is a space). The original
string is returned if width is less than len(s).

Changed in version 2.4: Support for the fillchar argument.

--
View this message in context: http://www.nabble.com/trailing-space-in-text-string-stripped%2C-making-it-impossible-to-right-pad-my-text-tp25639703p25653325.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Thanks. Unfortunately this gives me the same results: no good. Something is
stripping whatever filler is there on the right.

text(xlim()[1],1.01,'string'.ljust(10,' '),horizontalalignment='right')

John [H2O] wrote:

···

Christopher Barrington-Leigh wrote:

Hello. My problem is as follows:
(ipython --pylab)

from pylab import *
pp=plot([0,0],[1,1])
text(xlim()[0],1,' Need padding ',horizontalalignment='left')
text(xlim()[1],1,' Need padding ',horizontalalignment='right')

The second case does not do what I want, which is to pad the text on the
right. Text strings are stripped on the right, but no on the left. How
can I elegantly create a character of space?

Thanks!
c

Untested, but I think you could do this with just python builtin types:

from pylab import *
pp=plot([0,0],[1,1])
text(xlim()[0],1,'string'.ljust(10,' '))
text(xlim()[1],1,'string'.rjust(10,' '))

See the documention:
http://docs.python.org/library/stdtypes.html

str.ljust(width[, fillchar])¶

Return the string left justified in a string of length width. Padding is
done using the specified fillchar (default is a space). The original
string is returned if width is less than len(s).

Changed in version 2.4: Support for the fillchar argument.

--
View this message in context: http://www.nabble.com/trailing-space-in-text-string-stripped%2C-making-it-impossible-to-right-pad-my-text-tp25639703p25653331.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

All my attempts at using TeX spacing failed too. What do you have in mind?

e.g.
text(xlim()[1],1.02,r'$r^\;2$~~~',horizontalalignment='right')

Jae-Joon Lee wrote:

···

While I think you're not using usetex mode, you may use tex's own
spacing command with usetex mode. Depending on your need, you may also
use the annotate function with which you can provide a spacing in
pixel coordinate (or other coordinate the annotate supports).

--
View this message in context: http://www.nabble.com/trailing-space-in-text-string-stripped%2C-making-it-impossible-to-right-pad-my-text-tp25639703p25653573.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

You need an empty mbox at the end of line (\mbox{}).

text(xlim()[1],1.02,r'$r^2$~~~\mbox{}',horizontalalignment='right')

If you did mean \; after ^, it requires {}.

Again, these only works if you use real TeX for text rendering (ie
usetex=True), not the mpl's mathtext mode.

Regards,

-JJ

···

On Mon, Sep 28, 2009 at 5:48 PM, Christopher Barrington-Leigh <cpblpublic+nabble@...287...> wrote:

All my attempts at using TeX spacing failed too. What do you have in mind?

e.g.
text(xlim()[1],1.02,r'$r^\;2$~~~',horizontalalignment='right')

Jae-Joon Lee wrote:

While I think you're not using usetex mode, you may use tex's own
spacing command with usetex mode. Depending on your need, you may also
use the annotate function with which you can provide a spacing in
pixel coordinate (or other coordinate the annotate supports).

--
View this message in context: http://www.nabble.com/trailing-space-in-text-string-stripped%2C-making-it-impossible-to-right-pad-my-text-tp25639703p25653573.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Hmm, I'm afraid that this only works if you use preview mode. I
haven't tested, but I guess it will fail without it. Check if
rcParams["text.latex.preview"]==True.

-JJ

···

On Mon, Sep 28, 2009 at 6:26 PM, Jae-Joon Lee <lee.j.joon@...287...> wrote:

You need an empty mbox at the end of line (\mbox{}).

text(xlim()[1],1.02,r'$r^2$~~~\mbox{}',horizontalalignment='right')

If you did mean \; after ^, it requires {}.

Again, these only works if you use real TeX for text rendering (ie
usetex=True), not the mpl's mathtext mode.

Regards,

-JJ

On Mon, Sep 28, 2009 at 5:48 PM, Christopher Barrington-Leigh > <cpblpublic+nabble@...287...> wrote:

All my attempts at using TeX spacing failed too. What do you have in mind?

e.g.
text(xlim()[1],1.02,r'$r^\;2$~~~',horizontalalignment='right')

Jae-Joon Lee wrote:

While I think you're not using usetex mode, you may use tex's own
spacing command with usetex mode. Depending on your need, you may also
use the annotate function with which you can provide a spacing in
pixel coordinate (or other coordinate the annotate supports).

--
View this message in context: http://www.nabble.com/trailing-space-in-text-string-stripped%2C-making-it-impossible-to-right-pad-my-text-tp25639703p25653573.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Jae-Joon Lee wrote:

Hmm, I'm afraid that this only works if you use preview mode. I
haven't tested, but I guess it will fail without it. Check if
rcParams["text.latex.preview"]==True.

Hm, I don't know about mpl's mathtext mode, but I'm actually always
working in usetex mode. Unfortunately, even so,
"text.latex.preview" does not exist in rcParams for me. However,... :

I think the following behaviour is definitely
buggy! Please try the following one at a time, in ion() mode (or just see
sequence of png's produced). None of these succeeds in getting the desired
alignment.

from matplotlib import rc

figure(1)
clf()
plot([0,0],[1,1])

rc('text', usetex=True)

text(xlim()[1],.96,r'$r^2$~~~\mbox{}',horizontalalignment='right')
savefig('t10.png')

rc('text', usetex=False)
show()
savefig('t20.png')

text(xlim()[1],.97,r'$r^2$~~~\mbox{}',horizontalalignment='right')
savefig('t30.png')

rc('text', usetex=True)

show()
savefig('t40.png')

···

--
View this message in context: http://www.nabble.com/trailing-space-in-text-string-stripped%2C-making-it-impossible-to-right-pad-my-text-tp25639703p25656912.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Jae-Joon Lee wrote:

Hmm, I'm afraid that this only works if you use preview mode. I
haven't tested, but I guess it will fail without it. Check if
rcParams["text.latex.preview"]==True.

Hm, I don't know about mpl's mathtext mode, but I'm actually always
working in usetex mode. Unfortunately, even so,
"text.latex.preview" does not exist in rcParams for me. However,... :

I think the following behaviour is definitely
buggy! Please try the following one at a time, in ion() mode (or just see
sequence of png's produced). None of these succeeds in getting the desired
alignment.

from matplotlib import rc

figure(1)
clf()
plot([0,0],[1,1])

rc('text', usetex=True)

text(xlim()[1],.96,r'$r^2$~~~\mbox{}',horizontalalignment='right')
savefig('t10.png')

This works if you use recent version of matplotlib with preview mode
on. Without the preview mode (or other similar ways to report the
dimension of the text from TeX side), I don't think this can be done.

rc('text', usetex=False)
show()
savefig('t20.png')

text(xlim()[1],.97,r'$r^2$~~~\mbox{}',horizontalalignment='right')
savefig('t30.png')

This does not. First of all, "~" and "\mbox" are not supported if
usetex=False and I guess never will be. On the other hand,
as far as I can see, the whitespace stripping is not done in mpl
side. And I have a feeling that it may be the freetype library. mpl
uses FT_Glyph_Get_CBox to calculate the extents of the text and I
think this seems to fail when there is a trailing spaces. This is
beyond me I hope other developers have better idea.

So, Christopher,
I guess your best chance is to upgrade and use preview mode, or use
annotate function.

Regards,

-JJ

···

On Tue, Sep 29, 2009 at 12:53 AM, Christopher Barrington-Leigh <cpblpublic+nabble@...287...> wrote:

rc('text', usetex=True)

show()
savefig('t40.png')

--
View this message in context: http://www.nabble.com/trailing-space-in-text-string-stripped%2C-making-it-impossible-to-right-pad-my-text-tp25639703p25656912.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Jae-Joon Lee wrote:

This works if you use recent version of matplotlib with preview mode
on. Without the preview mode (or other similar ways to report the
dimension of the text from TeX side), I don't think this can be done.

Ok, thanks. I hope I am understanding. Would you be able to point me to a
link to info on what you mean by "preview mode"?
By recent version you mean something in the svn, I guess?

This does not. First of all, "~" and "\mbox" are not supported if
usetex=False and I guess never will be. On the other hand,
as far as I can see, the whitespace stripping is not done in mpl
side. And I have a feeling that it may be the freetype library. mpl
uses FT_Glyph_Get_CBox to calculate the extents of the text and I
think this seems to fail when there is a trailing spaces. This is
beyond me I hope other developers have better idea.

Thanks, I too hope some other developers chime in!

So, Christopher,
I guess your best chance is to upgrade and use preview mode, or use
annotate function.

I am not clear how annotate helps. If I am willing to specify x,y locations,
I can do that with text() as well, no?. It's still a matter of calculating
how big a piece of whitespace should end up, roughly.

Thanks, JJ!

···

--
View this message in context: http://www.nabble.com/trailing-space-in-text-string-stripped%2C-making-it-impossible-to-right-pad-my-text-tp25639703p25688584.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Jae-Joon Lee wrote:

This works if you use recent version of matplotlib with preview mode
on. Without the preview mode (or other similar ways to report the
dimension of the text from TeX side), I don't think this can be done.

Ok, thanks. I hope I am understanding. Would you be able to point me to a
link to info on what you mean by "preview mode"?
By recent version you mean something in the svn, I guess?

0.99.0 and later should work.

text.usetex : True
text.latex.preview : True

Include these in your rc file. While the preview.sty is installed in
most tex distribution, you may need to install it if not.

This does not. First of all, "~" and "\mbox" are not supported if
usetex=False and I guess never will be. On the other hand,
as far as I can see, the whitespace stripping is not done in mpl
side. And I have a feeling that it may be the freetype library. mpl
uses FT_Glyph_Get_CBox to calculate the extents of the text and I
think this seems to fail when there is a trailing spaces. This is
beyond me I hope other developers have better idea.

Thanks, I too hope some other developers chime in!

So, Christopher,
I guess your best chance is to upgrade and use preview mode, or use
annotate function.

I am not clear how annotate helps. If I am willing to specify x,y locations,
I can do that with text() as well, no?. It's still a matter of calculating
how big a piece of whitespace should end up, roughly.

annotate let you specify offset from (x, y).

annotate("test", (1., 0.5), (-20, 0), xycoords='data',
         textcoords='offset points', ha="right")

Check the documentation.

Regards,

-JJ

···

On Wed, Sep 30, 2009 at 4:38 PM, Christopher Barrington-Leigh <cpblpublic+nabble@...287...> wrote:

Thanks, JJ!

--
View this message in context: http://www.nabble.com/trailing-space-in-text-string-stripped%2C-making-it-impossible-to-right-pad-my-text-tp25639703p25688584.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Christopher Barrington-Leigh wrote:

This does not. First of all, "~" and "\mbox" are not supported if
usetex=False and I guess never will be. On the other hand,
as far as I can see, the whitespace stripping is not done in mpl
side. And I have a feeling that it may be the freetype library. mpl
uses FT_Glyph_Get_CBox to calculate the extents of the text and I
think this seems to fail when there is a trailing spaces. This is
beyond me I hope other developers have better idea.

Thanks, I too hope some other developers chime in!
  

The calculation of the text bounding box was only taking into account the outlines of the text -- and spaces don't have any outlines. I have updated this to also take into account the x-advance of each character, so it now works as expected. As this has the potential to break things, I did this on the trunk, not the maintenance branch, so it will make it into the next major release, not the next bugfix release.

The patch can be found here, if you wish to apply it locally:

http://matplotlib.svn.sourceforge.net/viewvc/matplotlib/trunk/matplotlib/src/ft2font.cpp?r1=7635&r2=7838

Cheers,
Mike

···

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

From: Christopher Barrington-Leigh
[mailto:cpblpublic+nabble@…287…]
Sent: Sunday, September 27, 2009 21:58

Hello. My problem is as follows:
(ipython --pylab)

from pylab import *
pp=plot([0,0],[1,1])
text(xlim()[0],1,' Need padding ',horizontalalignment='left')
text(xlim()[1],1,' Need padding ',horizontalalignment='right')

The second case does not do what I want, which is to pad the
text on the right. Text strings are stripped on the right,
but no on the left. How can I elegantly create a character of space?

Sorry to be chiming in somewhat late in the discussion, but below is a method
for padding that uses transforms instead of characters. It's based on a bit
of code in the cla() method of the Axes class. The helper functions
facilitate padding relative to data or axes coordinates, and the padding is
expressed in physical length units. (I picked inches just because the figure
dimensions are managed in inches.)

···

------------

import matplotlib.pyplot as plt
import matplotlib.transforms as transforms

def padded_data_transform(axes, xPadInches, yPadInches):
    return axes.transData + transforms.ScaledTranslation(xPadInches,
        yPadInches, axes.figure.dpi_scale_trans)

def padded_axes_transform(axes, xPadInches, yPadInches):
    return axes.transAxes + transforms.ScaledTranslation(xPadInches,
        yPadInches, axes.figure.dpi_scale_trans)

plt.plot([0], [1], '+')
ax = plt.gca()
plt.text(0, 1, '12 pt right of (0, 1)',
    horizontalalignment='left', verticalalignment='center',
    transform=padded_data_transform(ax, 12 / 72.0, 0))
plt.text(0, 1, '12 pt left of (0, 1)',
    horizontalalignment='right', verticalalignment='center',
    transform=padded_data_transform(ax, -12 / 72.0, 0))
plt.text(0, 0, '0.5 in right of lower left',
    horizontalalignment='left', verticalalignment='bottom',
    transform=padded_axes_transform(ax, 0.5, 0))
plt.text(1, 1, '0.5 in left of upper right',
    horizontalalignment='right', verticalalignment='top',
    transform=padded_axes_transform(ax, -0.5, 0))

------------