10
10
from matplotlib import rcParams
11
11
import matplotlib .artist as artist
12
12
from matplotlib .artist import Artist
13
- from matplotlib .cbook import is_string_like , maxdict
13
+ from matplotlib .cbook import is_string_like , maxdict , is_numlike
14
14
from matplotlib import docstring
15
15
from matplotlib .font_manager import FontProperties
16
16
from matplotlib .patches import bbox_artist , YAArrow , FancyBboxPatch , \
@@ -69,21 +69,21 @@ def get_rotation(rotation):
69
69
family [ 'serif' | 'sans-serif' | 'cursive' | 'fantasy' | 'monospace' ]
70
70
figure a matplotlib.figure.Figure instance
71
71
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 ]
73
73
label any string
74
74
linespacing float
75
75
lod [True | False]
76
76
multialignment ['left' | 'right' | 'center' ]
77
77
name or fontname string eg, ['Sans' | 'Courier' | 'Helvetica' ...]
78
78
position (x,y)
79
- rotation [ angle in degrees 'vertical' | 'horizontal'
79
+ rotation [ angle in degrees | 'vertical' | 'horizontal' ]
80
80
rotation_mode [ None | 'anchor']
81
81
size or fontsize [ size in points | relative size eg 'smaller', 'x-large' ]
82
82
style or fontstyle [ 'normal' | 'italic' | 'oblique']
83
83
text string
84
84
transform a matplotlib.transform transformation instance
85
85
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 ]
87
87
visible [True | False]
88
88
weight or fontweight [ 'normal' | 'bold' | 'heavy' | 'light' | 'ultrabold' | 'ultralight']
89
89
x float
@@ -378,25 +378,29 @@ def get_text_width_height_descent(*kl, **kwargs):
378
378
if rotation_mode != "anchor" :
379
379
# compute the text location in display coords and the offsets
380
380
# 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 )
382
382
elif halign == 'right' : offsetx = (xmin + width )
383
+ elif is_numlike (halign ): offsetx = xmin + halign * (xmax - xmin )
383
384
else : offsetx = xmin
384
385
385
- if valign == 'center' : offsety = (ymin + height / 2.0 )
386
+ if valign == 'center' : offsety = (ymin + height * 0.5 )
386
387
elif valign == 'top' : offsety = (ymin + height )
387
388
elif valign == 'baseline' : offsety = (ymin + height ) - baseline
389
+ elif is_numlike (valign ): offsety = ymin + valign * (ymax - ymin )
388
390
else : offsety = ymin
389
391
else :
390
392
xmin1 , ymin1 = cornersHoriz [0 ]
391
393
xmax1 , ymax1 = cornersHoriz [2 ]
392
394
393
- if halign == 'center' : offsetx = (xmin1 + xmax1 )/ 2.0
395
+ if halign == 'center' : offsetx = (xmin1 + xmax1 )* 0.5
394
396
elif halign == 'right' : offsetx = xmax1
397
+ elif is_numlike (halign ): offsetx = xmin1 + halign * (xmax1 - xmin1 )
395
398
else : offsetx = xmin1
396
399
397
- if valign == 'center' : offsety = (ymin1 + ymax1 )/ 2.0
400
+ if valign == 'center' : offsety = (ymin1 + ymax1 )* 0.5
398
401
elif valign == 'top' : offsety = ymax1
399
402
elif valign == 'baseline' : offsety = ymax1 - baseline
403
+ elif is_numlike (valign ): offsety = ymin1 + valign * (ymax1 - ymin1 )
400
404
else : offsety = ymin1
401
405
402
406
offsetx , offsety = M .transform_point ((offsetx , offsety ))
@@ -796,11 +800,11 @@ def set_horizontalalignment(self, align):
796
800
"""
797
801
Set the horizontal alignment to one of
798
802
799
- ACCEPTS: [ 'center' | 'right' | 'left' ]
803
+ ACCEPTS: [ 'center' | 'right' | 'left' | fraction text width from left ]
800
804
"""
801
805
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 ))
804
808
self ._horizontalalignment = align
805
809
806
810
def set_ma (self , align ):
@@ -811,7 +815,7 @@ def set_ma(self, align):
811
815
def set_multialignment (self , align ):
812
816
"""
813
817
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
815
819
and verticalalignment properties, but the multiline text within that
816
820
box can be
817
821
@@ -957,11 +961,11 @@ def set_verticalalignment(self, align):
957
961
"""
958
962
Set the vertical alignment
959
963
960
- ACCEPTS: [ 'center' | 'top' | 'bottom' | 'baseline' ]
964
+ ACCEPTS: [ 'center' | 'top' | 'bottom' | 'baseline' | fraction text height from bottom ]
961
965
"""
962
966
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 ))
965
969
966
970
self ._verticalalignment = align
967
971
0 commit comments