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

Skip to content

Support horizontalalignment in TextArea/AnchoredText. #18367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions lib/matplotlib/offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,10 +769,11 @@ def draw(self, renderer):

class TextArea(OffsetBox):
"""
The TextArea is contains a single Text instance. The text is
placed at (0, 0) with baseline+left alignment. The width and height
of the TextArea instance is the width and height of the its child
text.
The TextArea is a container artist for a single Text instance.

The text is placed at (0, 0) with baseline+left alignment, by default. The
width and height of the TextArea instance is the width and height of its
child text.
"""
def __init__(self, s,
textprops=None,
Expand Down Expand Up @@ -876,37 +877,43 @@ def get_offset(self):
def get_window_extent(self, renderer):
"""Return the bounding box in display space."""
w, h, xd, yd = self.get_extent(renderer)
ox, oy = self.get_offset() # w, h, xd, yd)
ox, oy = self.get_offset()
return mtransforms.Bbox.from_bounds(ox - xd, oy - yd, w, h)

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

bbox, info, d = self._text._get_layout(renderer)
bbox, info, yd = self._text._get_layout(renderer)
w, h = bbox.width, bbox.height

self._baseline_transform.clear()

if len(info) > 1 and self._multilinebaseline:
d_new = 0.5 * h - 0.5 * (h_ - d_)
self._baseline_transform.translate(0, d - d_new)
d = d_new
yd_new = 0.5 * h - 0.5 * (h_ - d_)
self._baseline_transform.translate(0, yd - yd_new)
yd = yd_new

else: # single line

h_d = max(h_ - d_, h - d)
h_d = max(h_ - d_, h - yd)

if self.get_minimumdescent():
## to have a minimum descent, #i.e., "l" and "p" have same
## descents.
d = max(d, d_)
#else:
# d = d
# To have a minimum descent, i.e., "l" and "p" have same
# descents.
yd = max(yd, d_)

h = h_d + d
h = h_d + yd

return w, h, 0., d
ha = self._text.get_horizontalalignment()
if ha == 'left':
xd = 0
elif ha == 'center':
xd = w / 2
elif ha == 'right':
xd = w

return w, h, xd, yd

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

if prop is None:
prop = {}
badkwargs = {'ha', 'horizontalalignment', 'va', 'verticalalignment'}
badkwargs = {'va', 'verticalalignment'}
if badkwargs & set(prop):
raise ValueError(
"Mixing horizontalalignment or verticalalignment with "
"AnchoredText is not supported.")
'Mixing verticalalignment with AnchoredText is not supported.')

self.txt = TextArea(s, textprops=prop, minimumdescent=False)
fp = self.txt._text.get_fontproperties()
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 17 additions & 2 deletions lib/matplotlib/tests/test_offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from matplotlib.backend_bases import MouseButton

from matplotlib.offsetbox import (
AnchoredOffsetbox, AnnotationBbox, DrawingArea, OffsetImage, TextArea,
_get_packed_offsets)
AnchoredOffsetbox, AnnotationBbox, AnchoredText, DrawingArea,
OffsetImage, TextArea, _get_packed_offsets)


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


@image_comparison(['anchoredtext_align.png'], remove_text=True, style='mpl20')
def test_anchoredtext_horizontal_alignment():
fig, ax = plt.subplots()

text0 = AnchoredText("test\ntest long text", loc="center left",
pad=0.2, prop={"ha": "left"})
ax.add_artist(text0)
text1 = AnchoredText("test\ntest long text", loc="center",
pad=0.2, prop={"ha": "center"})
ax.add_artist(text1)
text2 = AnchoredText("test\ntest long text", loc="center right",
pad=0.2, prop={"ha": "right"})
ax.add_artist(text2)


def test_annotationbbox_extents():
plt.rcParams.update(plt.rcParamsDefault)
fig, ax = plt.subplots(figsize=(4, 3), dpi=100)
Expand Down