1010from matplotlib import rcParams
1111import matplotlib .artist as artist
1212from matplotlib .artist import Artist
13- from matplotlib .cbook import is_string_like , maxdict
13+ from matplotlib .cbook import is_string_like , maxdict , is_numlike
1414from matplotlib import docstring
1515from matplotlib .font_manager import FontProperties
1616from matplotlib .patches import bbox_artist , YAArrow , FancyBboxPatch , \
@@ -69,21 +69,21 @@ def get_rotation(rotation):
6969 family [ 'serif' | 'sans-serif' | 'cursive' | 'fantasy' | 'monospace' ]
7070 figure a matplotlib.figure.Figure instance
7171 fontproperties a matplotlib.font_manager.FontProperties instance
72- horizontalalignment or ha [ 'center' | 'right' | 'left' ]
72+ horizontalalignment or ha [ 'center' | 'right' | 'left' | fraction text width from left ]
7373 label any string
7474 linespacing float
7575 lod [True | False]
7676 multialignment ['left' | 'right' | 'center' ]
7777 name or fontname string eg, ['Sans' | 'Courier' | 'Helvetica' ...]
7878 position (x,y)
79- rotation [ angle in degrees 'vertical' | 'horizontal'
79+ rotation [ angle in degrees | 'vertical' | 'horizontal' ]
8080 rotation_mode [ None | 'anchor']
8181 size or fontsize [ size in points | relative size eg 'smaller', 'x-large' ]
8282 style or fontstyle [ 'normal' | 'italic' | 'oblique']
8383 text string
8484 transform a matplotlib.transform transformation instance
8585 variant [ 'normal' | 'small-caps' ]
86- verticalalignment or va [ 'center' | 'top' | 'bottom' | 'baseline' ]
86+ verticalalignment or va [ 'center' | 'top' | 'bottom' | 'baseline' | fraction text height from bottom ]
8787 visible [True | False]
8888 weight or fontweight [ 'normal' | 'bold' | 'heavy' | 'light' | 'ultrabold' | 'ultralight']
8989 x float
@@ -378,25 +378,29 @@ def get_text_width_height_descent(*kl, **kwargs):
378378 if rotation_mode != "anchor" :
379379 # compute the text location in display coords and the offsets
380380 # necessary to align the bbox with that location
381- if halign == 'center' : offsetx = (xmin + width / 2.0 )
381+ if halign == 'center' : offsetx = (xmin + width * 0.5 )
382382 elif halign == 'right' : offsetx = (xmin + width )
383+ elif is_numlike (halign ): offsetx = xmin + halign * (xmax - xmin )
383384 else : offsetx = xmin
384385
385- if valign == 'center' : offsety = (ymin + height / 2.0 )
386+ if valign == 'center' : offsety = (ymin + height * 0.5 )
386387 elif valign == 'top' : offsety = (ymin + height )
387388 elif valign == 'baseline' : offsety = (ymin + height ) - baseline
389+ elif is_numlike (valign ): offsety = ymin + valign * (ymax - ymin )
388390 else : offsety = ymin
389391 else :
390392 xmin1 , ymin1 = cornersHoriz [0 ]
391393 xmax1 , ymax1 = cornersHoriz [2 ]
392394
393- if halign == 'center' : offsetx = (xmin1 + xmax1 )/ 2.0
395+ if halign == 'center' : offsetx = (xmin1 + xmax1 )* 0.5
394396 elif halign == 'right' : offsetx = xmax1
397+ elif is_numlike (halign ): offsetx = xmin1 + halign * (xmax1 - xmin1 )
395398 else : offsetx = xmin1
396399
397- if valign == 'center' : offsety = (ymin1 + ymax1 )/ 2.0
400+ if valign == 'center' : offsety = (ymin1 + ymax1 )* 0.5
398401 elif valign == 'top' : offsety = ymax1
399402 elif valign == 'baseline' : offsety = ymax1 - baseline
403+ elif is_numlike (valign ): offsety = ymin1 + valign * (ymax1 - ymin1 )
400404 else : offsety = ymin1
401405
402406 offsetx , offsety = M .transform_point ((offsetx , offsety ))
@@ -796,11 +800,11 @@ def set_horizontalalignment(self, align):
796800 """
797801 Set the horizontal alignment to one of
798802
799- ACCEPTS: [ 'center' | 'right' | 'left' ]
803+ ACCEPTS: [ 'center' | 'right' | 'left' | fraction text width from left ]
800804 """
801805 legal = ('center' , 'right' , 'left' )
802- if align not in legal :
803- raise ValueError ('Horizontal alignment must be one of %s' % str (legal ))
806+ if align not in legal and not is_numlike ( align ) :
807+ raise ValueError ('Horizontal alignment must be numeric or one of %s' % str (legal ))
804808 self ._horizontalalignment = align
805809
806810 def set_ma (self , align ):
@@ -811,7 +815,7 @@ def set_ma(self, align):
811815 def set_multialignment (self , align ):
812816 """
813817 Set the alignment for multiple lines layout. The layout of the
814- bounding box of all the lines is determined bu the horizontalalignment
818+ bounding box of all the lines is determined by the horizontalalignment
815819 and verticalalignment properties, but the multiline text within that
816820 box can be
817821
@@ -957,11 +961,11 @@ def set_verticalalignment(self, align):
957961 """
958962 Set the vertical alignment
959963
960- ACCEPTS: [ 'center' | 'top' | 'bottom' | 'baseline' ]
964+ ACCEPTS: [ 'center' | 'top' | 'bottom' | 'baseline' | fraction text height from bottom ]
961965 """
962966 legal = ('top' , 'bottom' , 'center' , 'baseline' )
963- if align not in legal :
964- raise ValueError ('Vertical alignment must be one of %s' % str (legal ))
967+ if align not in legal and not is_numlike ( align ) :
968+ raise ValueError ('Vertical alignment must be numeric or one of %s' % str (legal ))
965969
966970 self ._verticalalignment = align
967971
0 commit comments