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

Skip to content

Commit 50ec1c6

Browse files
committed
Support horizontalalignment in TextArea/AnchoredText.
Fixes #18363.
1 parent 8610965 commit 50ec1c6

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -769,10 +769,11 @@ def draw(self, renderer):
769769

770770
class TextArea(OffsetBox):
771771
"""
772-
The TextArea is contains a single Text instance. The text is
773-
placed at (0, 0) with baseline+left alignment. The width and height
774-
of the TextArea instance is the width and height of the its child
775-
text.
772+
The TextArea is a container artist for a single Text instance.
773+
774+
The text is placed at (0, 0) with baseline+left alignment, by default. The
775+
width and height of the TextArea instance is the width and height of its
776+
child text.
776777
"""
777778
def __init__(self, s,
778779
textprops=None,
@@ -876,37 +877,43 @@ def get_offset(self):
876877
def get_window_extent(self, renderer):
877878
"""Return the bounding box in display space."""
878879
w, h, xd, yd = self.get_extent(renderer)
879-
ox, oy = self.get_offset() # w, h, xd, yd)
880+
ox, oy = self.get_offset()
880881
return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h)
881882

882883
def get_extent(self, renderer):
883884
_, h_, d_ = renderer.get_text_width_height_descent(
884885
"lp", self._text._fontproperties, ismath=False)
885886

886-
bbox, info, d = self._text._get_layout(renderer)
887+
bbox, info, yd = self._text._get_layout(renderer)
887888
w, h = bbox.width, bbox.height
888889

889890
self._baseline_transform.clear()
890891

891892
if len(info) > 1 and self._multilinebaseline:
892-
d_new = 0.5 * h - 0.5 * (h_ - d_)
893-
self._baseline_transform.translate(0, d - d_new)
894-
d = d_new
893+
yd_new = 0.5 * h - 0.5 * (h_ - d_)
894+
self._baseline_transform.translate(0, yd - yd_new)
895+
yd = yd_new
895896

896897
else: # single line
897898

898-
h_d = max(h_ - d_, h - d)
899+
h_d = max(h_ - d_, h - yd)
899900

900901
if self.get_minimumdescent():
901-
## to have a minimum descent, #i.e., "l" and "p" have same
902-
## descents.
903-
d = max(d, d_)
904-
#else:
905-
# d = d
902+
# To have a minimum descent, i.e., "l" and "p" have same
903+
# descents.
904+
yd = max(yd, d_)
906905

907-
h = h_d + d
906+
h = h_d + yd
908907

909-
return w, h, 0., d
908+
ha = self._text.get_horizontalalignment()
909+
if ha == 'left':
910+
xd = 0
911+
elif ha == 'center':
912+
xd = w / 2
913+
elif ha == 'right':
914+
xd = w
915+
916+
return w, h, xd, yd
910917

911918
def draw(self, renderer):
912919
# docstring inherited
@@ -1288,11 +1295,10 @@ def __init__(self, s, loc, pad=0.4, borderpad=0.5, prop=None, **kwargs):
12881295

12891296
if prop is None:
12901297
prop = {}
1291-
badkwargs = {'ha', 'horizontalalignment', 'va', 'verticalalignment'}
1298+
badkwargs = {'va', 'verticalalignment'}
12921299
if badkwargs & set(prop):
12931300
raise ValueError(
1294-
"Mixing horizontalalignment or verticalalignment with "
1295-
"AnchoredText is not supported.")
1301+
'Mixing verticalalignment with AnchoredText is not supported.')
12961302

12971303
self.txt = TextArea(s, textprops=prop, minimumdescent=False)
12981304
fp = self.txt._text.get_fontproperties()

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from matplotlib.backend_bases import MouseButton
1313

1414
from matplotlib.offsetbox import (
15-
AnchoredOffsetbox, AnnotationBbox, DrawingArea, OffsetImage, TextArea,
16-
_get_packed_offsets)
15+
AnchoredOffsetbox, AnnotationBbox, AnchoredText, DrawingArea,
16+
OffsetImage, TextArea, _get_packed_offsets)
1717

1818

1919
@image_comparison(['offsetbox_clipping'], remove_text=True)
@@ -241,6 +241,21 @@ def test_picking(child_type, boxcoords):
241241
assert len(calls) == 0
242242

243243

244+
@image_comparison(['anchoredtext_align.png'], remove_text=True, style='mpl20')
245+
def test_anchoredtext_horizontal_alignment():
246+
fig, ax = plt.subplots()
247+
248+
text0 = AnchoredText("test\ntest long text", loc="center left",
249+
pad=0.2, prop={"ha": "left"})
250+
ax.add_artist(text0)
251+
text1 = AnchoredText("test\ntest long text", loc="center",
252+
pad=0.2, prop={"ha": "center"})
253+
ax.add_artist(text1)
254+
text2 = AnchoredText("test\ntest long text", loc="center right",
255+
pad=0.2, prop={"ha": "right"})
256+
ax.add_artist(text2)
257+
258+
244259
def test_annotationbbox_extents():
245260
plt.rcParams.update(plt.rcParamsDefault)
246261
fig, ax = plt.subplots(figsize=(4, 3), dpi=100)

0 commit comments

Comments
 (0)