Skip to content

Text alignment #1180

Closed
Closed

Description

@2sn

Hi,

I added a quick patch to allow numerical values for text alignment.
It now allows a number that is fraction of text hight or with and is measured relative to bottom or left. So
ha = 0 == 'left'
ha = 1 == 'right'
va = 0 == 'bottom'
va = 1 == 'top'
va/ha = 0.5 == 'center'

the cool thing is that values outside [0,1] allow you to some defined distance from a line or symbol that remains well-measured even if you interactively scale the figure.

Can you please add this to MPL?

(also fixed some typos in typo doc)

This is my first addition here, not sure how to add patches, I can submit a pull request it appears.

-Alexander

2sn/matplotlib@master...text-numeric-va-ha

From b167849f438e4b77319c51fd603946525823edc0 Mon Sep 17 00:00:00 2001
From: Alexander Heger matplotlib@2sn.net
Date: Fri, 31 Aug 2012 16:58:04 +1000
Subject: [PATCH 1/2] ADD - added numeric update for ha and va in Text


lib/matplotlib/text.py | 34 +++++++++++++++++++---------------
1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py
index b00e4cf..b1188de 100644
--- a/lib/matplotlib/text.py
+++ b/lib/matplotlib/text.py
@@ -10,7 +10,7 @@ from matplotlib import cbook
from matplotlib import rcParams
import matplotlib.artist as artist
from matplotlib.artist import Artist
-from matplotlib.cbook import is_string_like, maxdict
+from matplotlib.cbook import is_string_like, maxdict, is_numlike
from matplotlib import docstring
from matplotlib.font_manager import FontProperties
from matplotlib.patches import bbox_artist, YAArrow, FancyBboxPatch,
@@ -69,21 +69,21 @@ docstring.interpd.update(Text = """
family [ 'serif' | 'sans-serif' | 'cursive' | 'fantasy' | 'monospace' ]
figure a matplotlib.figure.Figure instance
fontproperties a matplotlib.font_manager.FontProperties instance

  • horizontalalignment or ha [ 'center' | 'right' | 'left' ]

  • horizontalalignment or ha [ 'center' | 'right' | 'left' | fraction text width from left ]
    label any string
    linespacing float
    lod [True | False]
    multialignment ['left' | 'right' | 'center' ]
    name or fontname string eg, ['Sans' | 'Courier' | 'Helvetica' ...]
    position (x,y)

  • rotation [ angle in degrees 'vertical' | 'horizontal'

  • rotation [ angle in degrees | 'vertical' | 'horizontal' ]
    rotation_mode [ None | 'anchor']
    size or fontsize [ size in points | relative size eg 'smaller', 'x-large' ]
    style or fontstyle [ 'normal' | 'italic' | 'oblique']
    text string
    transform a matplotlib.transform transformation instance
    variant [ 'normal' | 'small-caps' ]

  • verticalalignment or va [ 'center' | 'top' | 'bottom' | 'baseline' ]

  • verticalalignment or va [ 'center' | 'top' | 'bottom' | 'baseline' | fraction text height from bottom ]
    visible [True | False]
    weight or fontweight [ 'normal' | 'bold' | 'heavy' | 'light' | 'ultrabold' | 'ultralight']
    x float
    @@ -378,25 +378,29 @@ class Text(Artist):
    if rotation_mode != "anchor":
    # compute the text location in display coords and the offsets
    # necessary to align the bbox with that location

  •        if halign=='center':  offsetx = (xmin + width/2.0)
    
  •        if halign=='center':  offsetx = (xmin + width*0.5)
         elif halign=='right': offsetx = (xmin + width)
    
  •        elif is_numlike(halign): offsetx = xmin + halign * (xmax - xmin)
         else: offsetx = xmin
    
  •        if valign=='center': offsety = (ymin + height/2.0)
    
  •        if valign=='center': offsety = (ymin + height*0.5)
         elif valign=='top': offsety  = (ymin + height)
         elif valign=='baseline': offsety = (ymin + height) - baseline
    
  •        elif is_numlike(valign): offsety = ymin + valign * (ymax - ymin)
         else: offsety = ymin
     else:
         xmin1, ymin1 = cornersHoriz[0]
         xmax1, ymax1 = cornersHoriz[2]
    
  •        if halign=='center':  offsetx = (xmin1 + xmax1)/2.0
    
  •        if halign=='center':  offsetx = (xmin1 + xmax1)*0.5
         elif halign=='right': offsetx = xmax1
    
  •        elif is_numlike(halign): offsetx = xmin1 + halign * (xmax1 - xmin1)
         else: offsetx = xmin1
    
  •        if valign=='center': offsety = (ymin1 + ymax1)/2.0
    
  •        if valign=='center': offsety = (ymin1 + ymax1)*0.5
         elif valign=='top': offsety  = ymax1
         elif valign=='baseline': offsety = ymax1 - baseline
    
  •        elif is_numlike(valign): offsety = ymin1 + valign * (ymax1 - ymin1)
         else: offsety = ymin1
    
         offsetx, offsety = M.transform_point((offsetx, offsety))
    

    @@ -796,11 +800,11 @@ class Text(Artist):
    """
    Set the horizontal alignment to one of

  •    ACCEPTS: [ 'center' | 'right' | 'left' ]
    
  •    ACCEPTS: [ 'center' | 'right' | 'left' | fraction text width from left ]
     """
     legal = ('center', 'right', 'left')
    
  •    if align not in legal:
    
  •        raise ValueError('Horizontal alignment must be one of %s' % str(legal))
    
  •    if align not in legal and not is_numlike(align):
    
  •        raise ValueError('Horizontal alignment must be numeric or one of %s' % str(legal))
     self._horizontalalignment = align
    

    def set_ma(self, align):
    @@ -811,7 +815,7 @@ class Text(Artist):
    def set_multialignment(self, align):
    """
    Set the alignment for multiple lines layout. The layout of the

  •    bounding box of all the lines is determined bu the horizontalalignment
    
  •    bounding box of all the lines is determined but the horizontalalignment
     and verticalalignment properties, but the multiline text within that
     box can be
    

@@ -957,11 +961,11 @@ class Text(Artist):
"""
Set the vertical alignment

  •    ACCEPTS: [ 'center' | 'top' | 'bottom' | 'baseline' ]
    
  •    ACCEPTS: [ 'center' | 'top' | 'bottom' | 'baseline' | fraction text height from bottom ]
     """
     legal = ('top', 'bottom', 'center', 'baseline')
    
  •    if align not in legal:
    
  •        raise ValueError('Vertical alignment must be one of %s' % str(legal))
    
  •    if align not in legal and not is_numlike(align):
    
  •        raise ValueError('Vertical alignment must be numeric or one of %s' % str(legal))
    
     self._verticalalignment = align
    

1.7.11.4

From 076d2ef17d8bb25417ee805a9b9a9221918d230f Mon Sep 17 00:00:00 2001
From: Alexander Heger matplotlib@2sn.net
Date: Fri, 31 Aug 2012 17:05:23 +1000
Subject: [PATCH 2/2] BF typo


lib/matplotlib/text.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py
index b1188de..a22dd64 100644
--- a/lib/matplotlib/text.py
+++ b/lib/matplotlib/text.py
@@ -815,7 +815,7 @@ class Text(Artist):
def set_multialignment(self, align):
"""
Set the alignment for multiple lines layout. The layout of the

  •    bounding box of all the lines is determined but the horizontalalignment
    
  •    bounding box of all the lines is determined by the horizontalalignment
     and verticalalignment properties, but the multiline text within that
     box can be
    

1.7.11.4

Activity

  1. efiring commented on Jul 22, 2013

    @efiring
    Member

    @2sn Did you submit a pull request for this? If so, was it merged?

  2. efiring commented on Jul 22, 2013

    @efiring
    Member

    @2sn, Now I see you did: #1181. Therefore I will close this.

  3. 2sn commented on Jul 22, 2013

    @2sn
    Author

    Dear Eric,

    was it included then?
    I just made my own copy to overwrite.

    -Alexander

    On 22/07/13 12:46, Eric Firing wrote:

    @2sn https://github.com/2sn, Now I see you did: #1181
    #1181. Therefore I will
    close this.

    —
    Reply to this email directly or view it on GitHub
    #1180 (comment).

  4. efiring commented on Jul 22, 2013

    @efiring
    Member

    The PR is still open. The discussion seems to have raised some unresolved questions as to how to proceed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions