8000 Cleanup/Simplify Cell._set_text_position. · matplotlib/matplotlib@9f99995 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f99995

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 9f99995

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

lib/matplotlib/table.py

Lines changed: 16 additions & 22 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)
@@ -129,28 +129,22 @@ def draw(self, renderer):
129129
self.stale = False
130130

131131
def _set_text_position(self, renderer):
132-
"""Set text up so it draws in the right place.
132+
"""
133+
Set text up so it draws in the right place.
133134
134-
Currently support 'left', 'center' and 'right'
135+
Text location should be one of 'left', 'center' and 'right'.
135136
"""
136137
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-
138+
# center vertically
139+
y = bbox.y0 + bbox.height / 2
140+
# position horizontally
141+
loc = self._text.get_horizontalalignment()
142+
if loc == 'center':
143+
x = bbox.x0 + bbox.width / 2
144+
elif loc == 'left':
145+
x = bbox.x0 + bbox.width * self.PAD
146+
else: # right.
147+
x = bbox.x0 + bbox.width * (1 - self.PAD)
154148
self._text.set_position((x, y))
155149

156150
def get_text_bounds(self, renderer):

0 commit comments

Comments
 (0)
0