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

Skip to content

Commit 6ff1f82

Browse files
committed
Cleanup/Simplify Cell._set_text_position.
Directly pass *loc* to the text `horizontalalignment` in the constructor, which will avoid handling anything-but-"center"-and-"left" as "right", but error out (from the constructor, rather than at draw time) instead. Also, this means that changing the text alignment from `set_text_props` will work "as expected". Make the docstring style-compliant.
1 parent 0ba2b40 commit 6ff1f82

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

lib/matplotlib/table.py

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ def __init__(self, xy, width, height,
7979
if loc is None:
8080
loc = 'right'
8181
self._loc = loc
82-
self._text = Text(x=xy[0], y=xy[1], text=text,
83-
fontproperties=fontproperties)
84-
self._text.set_clip_on(False)
82+
self._text = Text(x=xy[0], y=xy[1], clip_on=False,
83+
text=text, fontproperties=fontproperties,
84+
horizontalalignment=loc, verticalalignment='center')
8585

8686
def set_transform(self, trans):
8787
Rectangle.set_transform(self, trans)
@@ -122,35 +122,24 @@ def draw(self, renderer):
122122
return
123123
# draw the rectangle
124124
Rectangle.draw(self, renderer)
125-
126125
# position the text
127126
self._set_text_position(renderer)
128127
self._text.draw(renderer)
129128
self.stale = False
130129

131130
def _set_text_position(self, renderer):
132-
"""Set text up so it draws in the right place.
133-
134-
Currently support 'left', 'center' and 'right'
135-
"""
131+
"""Set text up so it is drawn in the right place."""
136132
bbox = self.get_window_extent(renderer)
137-
l, b, w, h = bbox.bounds
138-
139-
# draw in center vertically
140-
self._text.set_verticalalignment('center')
141-
y = b + (h / 2.0)
142-
143-
# now position horizontally
144-
if self._loc == 'center':
145-
self._text.set_horizontalalignment('center')
146-
x = l + (w / 2.0)
147-
elif self._loc == 'left':
148-
self._text.set_horizontalalignment('left')
149-
x = l + (w * self.PAD)
150-
else:
151-
self._text.set_horizontalalignment('right')
152-
x = l + (w * (1.0 - self.PAD))
153-
133+
# center vertically
134+
y = bbox.y0 + bbox.height / 2
135+
# position horizontally
136+
loc = self._text.get_horizontalalignment()
137+
if loc == 'center':
138+
x = bbox.x0 + bbox.width / 2
139+
elif loc == 'left':
140+
x = bbox.x0 + bbox.width * self.PAD
141+
else: # right.
142+
x = bbox.x0 + bbox.width * (1 - self.PAD)
154143
self._text.set_position((x, y))
155144

156145
def get_text_bounds(self, renderer):

0 commit comments

Comments
 (0)