Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb17040

Browse files
authored
Merge pull request #9832 from anntzer/text
Minor cleanup to Text class.
2 parents cefc5c7 + 3f9d67f commit fb17040

File tree

1 file changed

+40
-55
lines changed

1 file changed

+40
-55
lines changed

lib/matplotlib/text.py

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
def _process_text_args(override, fontdict=None, **kwargs):
27-
"Return an override dict. See :func:`~pyplot.text' docstring for info"
27+
"""Return an override dict. See `~pyplot.text' docstring for info."""
2828

2929
if fontdict is not None:
3030
override.update(fontdict)
@@ -51,24 +51,21 @@ def _wrap_text(textobj):
5151
# Extracted from Text's method to serve as a function
5252
def get_rotation(rotation):
5353
"""
54-
Return the text angle as float. The returned
55-
angle is between 0 and 360 deg.
54+
Return the text angle as float between 0 and 360 degrees.
5655
5756
*rotation* may be 'horizontal', 'vertical', or a numeric value in degrees.
5857
"""
5958
try:
60-
angle = float(rotation)
59+
return float(rotation) % 360
6160
except (ValueError, TypeError):
6261
if cbook._str_equal(rotation, 'horizontal') or rotation is None:
63-
angle = 0.
62+
return 0.
6463
elif cbook._str_equal(rotation, 'vertical'):
65-
angle = 90.
64+
return 90.
6665
else:
67-
raise ValueError("rotation is {0} expected either 'horizontal'"
68-
" 'vertical', numeric value or"
69-
"None".format(rotation))
70-
71-
return angle % 360
66+
raise ValueError("rotation is {!r}; expected either 'horizontal', "
67+
"'vertical', numeric value, or None"
68+
.format(rotation))
7269

7370

7471
def _get_textbox(text, renderer):
@@ -104,9 +101,7 @@ def _get_textbox(text, renderer):
104101
xt_box, yt_box = min(projected_xs), min(projected_ys)
105102
w_box, h_box = max(projected_xs) - xt_box, max(projected_ys) - yt_box
106103

107-
tr = Affine2D().rotate(theta)
108-
109-
x_box, y_box = tr.transform_point((xt_box, yt_box))
104+
x_box, y_box = Affine2D().rotate(theta).transform_point((xt_box, yt_box))
110105

111106
return x_box, y_box, w_box, h_box
112107

@@ -125,9 +120,8 @@ def _get_textbox(text, renderer):
125120
"weight": ["fontweight"],
126121
})
127122
class Text(Artist):
128-
"""
129-
Handle storing and drawing of text in window or data coordinates.
130-
"""
123+
"""Handle storing and drawing of text in window or data coordinates."""
124+
131125
zorder = 3
132126
_cached = cbook.maxdict(50)
133127

@@ -149,8 +143,7 @@ def __init__(self,
149143
**kwargs
150144
):
151145
"""
152-
Create a :class:`~matplotlib.text.Text` instance at *x*, *y*
153-
with string *text*.
146+
Create a `Text` instance at *x*, *y* with string *text*.
154147
155148
Valid kwargs are
156149
%(Text)s
@@ -232,7 +225,9 @@ def contains(self, mouseevent):
232225
return inside, cattr
233226

234227
def _get_xy_display(self):
235-
'get the (possibly unit converted) transformed x, y in display coords'
228+
"""
229+
Get the (possibly unit converted) transformed x, y in display coords.
230+
"""
236231
x, y = self.get_unitless_position()
237232
return self.get_transform().transform_point((x, y))
238233

@@ -243,7 +238,7 @@ def _get_multialignment(self):
243238
return self._horizontalalignment
244239

245240
def get_rotation(self):
246-
'return the text angle as float in degrees'
241+
"""Return the text angle as float in degrees."""
247242
return get_rotation(self._rotation) # string_or_number -> number
248243

249244
def set_rotation_mode(self, m):
@@ -266,11 +261,11 @@ def set_rotation_mode(self, m):
266261
self.stale = True
267262

268263
def get_rotation_mode(self):
269-
"get text rotation mode"
264+
"""Get the text rotation mode."""
270265
return self._rotation_mode
271266

272267
def update_from(self, other):
273-
'Copy properties from other to self'
268+
"""Copy properties from other to self."""
274269
Artist.update_from(self, other)
275270
self._color = other._color
276271
self._multialignment = other._multialignment
@@ -481,16 +476,16 @@ def set_bbox(self, rectprops):
481476

482477
def get_bbox_patch(self):
483478
"""
484-
Return the bbox Patch object. Returns None if the
485-
FancyBboxPatch is not made.
479+
Return the bbox Patch, or None if the FancyBboxPatch is not made.
486480
"""
487481
return self._bbox_patch
488482

489483
def update_bbox_position_size(self, renderer):
490484
"""
491-
Update the location and the size of the bbox. This method
492-
should be used when the position and size of the bbox needs to
493-
be updated before actually drawing the bbox.
485+
Update the location and the size of the bbox.
486+
487+
This method should be used when the position and size of the bbox needs
488+
to be updated before actually drawing the bbox.
494489
"""
495490

496491
if self._bbox_patch:
@@ -514,9 +509,8 @@ def update_bbox_position_size(self, renderer):
514509
self._bbox_patch.set_mutation_scale(fontsize_in_pixel)
515510

516511
def _draw_bbox(self, renderer, posx, posy):
517-
518-
""" Update the location and the size of the bbox
519-
(FancyBboxPatch), and draw
512+
"""
513+
Update the location and size of the bbox (FancyBboxPatch), and draw.
520514
"""
521515

522516
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
@@ -533,7 +527,6 @@ def _update_clip_properties(self):
533527
clipprops = dict(clip_box=self.clipbox,
534528
clip_path=self._clippath,
535529
clip_on=self._clipon)
536-
537530
if self._bbox_patch:
538531
bbox = self._bbox_patch.update(clipprops)
539532

@@ -586,11 +579,11 @@ def set_clip_on(self, b):
586579
self._update_clip_properties()
587580

588581
def get_wrap(self):
589-
"""Returns the wrapping state for the text."""
582+
"""Return the wrapping state for the text."""
590583
return self._wrap
591584

592585
def set_wrap(self, wrap):
593-
"""Sets the wrapping state for the text.
586+
"""Set the wrapping state for the text.
594587
595588
Parameters
596589
----------
@@ -601,8 +594,8 @@ def set_wrap(self, wrap):
601594

602595
def _get_wrap_line_width(self):
603596
"""
604-
Returns the maximum line width for wrapping text based on the
605-
current orientation.
597+
Return the maximum line width for wrapping text based on the current
598+
orientation.
606599
"""
607600
x0, y0 = self.get_transform().transform(self.get_position())
608601
figure_box = self.get_figure().get_window_extent()
@@ -614,10 +607,7 @@ def _get_wrap_line_width(self):
614607

615608
left = self._get_dist_to_box(rotation, x0, y0, figure_box)
616609
right = self._get_dist_to_box(
617-
(180 + rotation) % 360,
618-
x0,
619-
y0,
620-
figure_box)
610+
(180 + rotation) % 360, x0, y0, figure_box)
621611

622612
if alignment == 'left':
623613
line_width = left
@@ -630,8 +620,8 @@ def _get_wrap_line_width(self):
630620

631621
def _get_dist_to_box(self, rotation, x0, y0, figure_box):
632622
"""
633-
Returns the distance from the given points, to the boundaries
634-
of a rotated box in pixels.
623+
Return the distance from the given points to the boundaries of a
624+
rotated box, in pixels.
635625
"""
636626
if rotation > 270:
637627
quad = rotation - 270
@@ -653,7 +643,7 @@ def _get_dist_to_box(self, rotation, x0, y0, figure_box):
653643

654644
def _get_rendered_text_width(self, text):
655645
"""
656-
Returns the width of a given text string, in pixels.
646+
Return the width of a given text string, in pixels.
657647
"""
658648
w, h, d = self._renderer.get_text_width_height_descent(
659649
text,
@@ -1228,7 +1218,7 @@ class TextWithDash(Text):
12281218
__name__ = 'textwithdash'
12291219

12301220
def __str__(self):
1231-
return "TextWithDash(%g,%g,%s)" % (self._x, self._y, repr(self._text))
1221+
return "TextWithDash(%g, %g, %r)" % (self._x, self._y, self._text)
12321222

12331223
def __init__(self,
12341224
x=0, y=0, text='',
@@ -1855,9 +1845,7 @@ def draggable(self, state=None, use_blit=False):
18551845

18561846
class Annotation(Text, _AnnotationBase):
18571847
def __str__(self):
1858-
return "Annotation(%g,%g,%s)" % (self.xy[0],
1859-
self.xy[1],
1860-
repr(self._text))
1848+
return "Annotation(%g, %g, %r)" % (self.xy[0], self.xy[1], self._text)
18611849

18621850
@docstring.dedent_interpd
18631851
def __init__(self, s, xy,
@@ -1876,10 +1864,10 @@ def __init__(self, s, xy,
18761864
----------
18771865
18781866
s : str
1879-
The text of the annotation
1867+
The text of the annotation.
18801868
18811869
xy : iterable
1882-
Length 2 sequence specifying the *(x,y)* point to annotate
1870+
Length 2 sequence specifying the *(x,y)* point to annotate.
18831871
18841872
xytext : iterable, optional
18851873
Length 2 sequence specifying the *(x,y)* to place the text
@@ -2089,15 +2077,13 @@ def set_figure(self, fig):
20892077
Artist.set_figure(self, fig)
20902078

20912079
def update_positions(self, renderer):
2092-
""""Update the pixel positions of the annotated point and the
2093-
text.
2094-
"""
2080+
"""Update the pixel positions of the annotated point and the text."""
20952081
xy_pixel = self._get_position_xy(renderer)
20962082
self._update_position_xytext(renderer, xy_pixel)
20972083

20982084
def _update_position_xytext(self, renderer, xy_pixel):
2099-
"""Update the pixel positions of the annotation text and the arrow
2100-
patch.
2085+
"""
2086+
Update the pixel positions of the annotation text and the arrow patch.
21012087
"""
21022088
# generate transformation,
21032089
self.set_transform(self._get_xy_transform(renderer, self.anncoords))
@@ -2236,7 +2222,6 @@ def get_window_extent(self, renderer=None):
22362222
simpler to call the method after saving the figure. The
22372223
*dpi* used defaults to self.figure.dpi; the renderer dpi is
22382224
irrelevant.
2239-
22402225
'''
22412226
if not self.get_visible():
22422227
return Bbox.unit()

0 commit comments

Comments
 (0)