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