Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 47f741e

Browse filesBrowse files
committed
Text cleanups.
Fix the docstring of _get_textbox. (`Text.get_extents` doesn't exist.) Inline `_draw_bbox` into its sole call site (the first half is the same as `update_bbox_position_size`; this does mean calling `convert_xunits/convert_yunits` twice but that should be cheap compared to everything else). `Annotation.draw` doesn't need to also call `update_bbox_position_size` as `Text.draw` will do it (I think the call was duplicated because the draw-a-bbox functionality was present in Annotation before it also moved to Text). rcParams -> mpl.rcParams. Remove an extraneous import.
1 parent 5173cda commit 47f741e
Copy full SHA for 47f741e

File tree

Expand file treeCollapse file tree

1 file changed

+16
-34
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-34
lines changed

‎lib/matplotlib/text.py

Copy file name to clipboardExpand all lines: lib/matplotlib/text.py
+16-34Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
import numpy as np
1111

12-
from . import _api, artist, cbook, docstring, rcParams
12+
import matplotlib as mpl
13+
from . import _api, artist, cbook, docstring
1314
from .artist import Artist
1415
from .font_manager import FontProperties
1516
from .patches import FancyArrowPatch, FancyBboxPatch, Rectangle
@@ -65,12 +66,13 @@ def get_rotation(rotation):
6566

6667
def _get_textbox(text, renderer):
6768
"""
68-
Calculate the bounding box of the text. Unlike
69-
:meth:`matplotlib.text.Text.get_extents` method, The bbox size of
70-
the text before the rotation is calculated.
69+
Calculate the bounding box of the text.
70+
71+
The bbox position takes text rotation into account, but the width and
72+
height are those of the unrotated box (unlike `.Text.get_window_extent`).
7173
"""
7274
# TODO : This function may move into the Text class as a method. As a
73-
# matter of fact, The information from the _get_textbox function
75+
# matter of fact, the information from the _get_textbox function
7476
# should be available during the Text._get_layout() call, which is
7577
# called within the _get_textbox. So, it would better to move this
7678
# function as a method with some refactoring of _get_layout method.
@@ -150,7 +152,8 @@ def __init__(self,
150152
self._x, self._y = x, y
151153
self._text = ''
152154
self.set_text(text)
153-
self.set_color(color if color is not None else rcParams["text.color"])
155+
self.set_color(
156+
color if color is not None else mpl.rcParams["text.color"])
154157
self.set_fontproperties(fontproperties)
155158
self.set_usetex(usetex)
156159
self.set_wrap(wrap)
@@ -490,17 +493,12 @@ def update_bbox_position_size(self, renderer):
490493
This method should be used when the position and size of the bbox needs
491494
to be updated before actually drawing the bbox.
492495
"""
493-
494496
if self._bbox_patch:
495-
496-
trans = self.get_transform()
497-
498497
# don't use self.get_unitless_position here, which refers to text
499498
# position in Text:
500499
posx = float(self.convert_xunits(self._x))
501500
posy = float(self.convert_yunits(self._y))
502-
503-
posx, posy = trans.transform((posx, posy))
501+
posx, posy = self.get_transform().transform((posx, posy))
504502

505503
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
506504
self._bbox_patch.set_bounds(0., 0., w_box, h_box)
@@ -511,22 +509,6 @@ def update_bbox_position_size(self, renderer):
511509
fontsize_in_pixel = renderer.points_to_pixels(self.get_size())
512510
self._bbox_patch.set_mutation_scale(fontsize_in_pixel)
513511

514-
def _draw_bbox(self, renderer, posx, posy):
515-
"""
516-
Update the location and size of the bbox (`.patches.FancyBboxPatch`),
517-
and draw.
518-
"""
519-
520-
x_box, y_box, w_box, h_box = _get_textbox(self, renderer)
521-
self._bbox_patch.set_bounds(0., 0., w_box, h_box)
522-
theta = np.deg2rad(self.get_rotation())
523-
tr = Affine2D().rotate(theta)
524-
tr = tr.translate(posx + x_box, posy + y_box)
525-
self._bbox_patch.set_transform(tr)
526-
fontsize_in_pixel = renderer.points_to_pixels(self.get_size())
527-
self._bbox_patch.set_mutation_scale(fontsize_in_pixel)
528-
self._bbox_patch.draw(renderer)
529-
530512
def _update_clip_properties(self):
531513
clipprops = dict(clip_box=self.clipbox,
532514
clip_path=self._clippath,
@@ -697,9 +679,11 @@ def draw(self, renderer):
697679
return
698680
canvasw, canvash = renderer.get_canvas_width_height()
699681

700-
# draw the FancyBboxPatch
682+
# Update the location and size of the bbox
683+
# (`.patches.FancyBboxPatch`), and draw it.
701684
if textobj._bbox_patch:
702-
textobj._draw_bbox(renderer, posx, posy)
685+
self.update_bbox_position_size(renderer)
686+
self._bbox_patch.draw(renderer)
703687

704688
gc = renderer.new_gc()
705689
gc.set_foreground(textobj.get_color())
@@ -721,7 +705,7 @@ def draw(self, renderer):
721705
if textobj.get_path_effects():
722706
from matplotlib.patheffects import PathEffectRenderer
723707
textrenderer = PathEffectRenderer(
724-
textobj.get_path_effects(), renderer)
708+
textobj.get_path_effects(), renderer)
725709
else:
726710
textrenderer = renderer
727711

@@ -1262,7 +1246,7 @@ def set_usetex(self, usetex):
12621246
:rc:`text.usetex`.
12631247
"""
12641248
if usetex is None:
1265-
self._usetex = rcParams['text.usetex']
1249+
self._usetex = mpl.rcParams['text.usetex']
12661250
else:
12671251
self._usetex = bool(usetex)
12681252
self.stale = True
@@ -1461,7 +1445,6 @@ def _get_xy_transform(self, renderer, s):
14611445
if xy0 is not None:
14621446
# reference x, y in display coordinate
14631447
ref_x, ref_y = xy0
1464-
from matplotlib.transforms import Affine2D
14651448
if unit == "points":
14661449
# dots per points
14671450
dpp = self.figure.get_dpi() / 72.
@@ -1942,7 +1925,6 @@ def draw(self, renderer):
19421925
if not self.get_visible() or not self._check_xy(renderer):
19431926
return
19441927
self.update_positions(renderer)
1945-
self.update_bbox_position_size(renderer)
19461928
if self.arrow_patch is not None: # FancyArrowPatch
19471929
if self.arrow_patch.figure is None and self.figure is not None:
19481930
self.arrow_patch.figure = self.figure

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.