8000 Merge pull request #24570 from greglucas/packer-align · matplotlib/matplotlib@116ace5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 116ace5

Browse files
authored
Merge pull request #24570 from greglucas/packer-align
2 parents 8f0003a + b51c471 commit 116ace5

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``HPacker`` alignment with **bottom** or **top** are now correct
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Previously, the **bottom** and **top** alignments were swapped.
5+
This has been corrected so that the alignments correspond appropriately.

lib/matplotlib/offsetbox.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ def _get_aligned_offsets(hd_list, height, align="baseline"):
166166
descent = max(d for h, d in hd_list)
167167
height = height_descent + descent
168168
offsets = [0. for h, d in hd_list]
169-
elif align in ["left", "top"]:
169+
elif align in ["left", "bottom"]:
170170
descent = 0.
171171
offsets = [d for h, d in hd_list]
172-
elif align in ["right", "bottom"]:
172+
elif align in ["right", "top"]:
173173
descent = 0.
174174
offsets = [height - h + d for h, d in hd_list]
175175
elif align == "center":

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

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

1818

1919
@image_comparison(['offsetbox_clipping'], remove_text=True)
@@ -335,3 +335,46 @@ def test_arrowprops_copied():
335335
arrowprops=arrowprops)
336336
assert ab.arrowprops is not ab
337337
assert arrowprops["relpos"] == (.3, .7)
338+
339+
340+
@pytest.mark.parametrize("align", ["baseline", "bottom", "top",
341+
"left", "right", "center"])
342+
def test_packers(align):
343+
# set the DPI to match points to make the math easier below
344+
fig = plt.figure(dpi=72)
345+
x1, y1 = 10, 30
346+
x2, y2 = 20, 60
347+
r1 = DrawingArea(x1, y1)
348+
r2 = DrawingArea(x2, y2)
349+
350+
hpacker = HPacker(children=[r1, r2], pad=0, sep=0, align=align)
351+
vpacker = VPacker(children=[r1, r2], pad=0, sep=0, align=align)
352+
renderer = fig.canvas.get_renderer()
353+
354+
# HPacker
355+
*extents, offset_pairs = hpacker.get_extent_offsets(renderer)
356+
# width, height, xdescent, ydescent
357+
assert_allclose((x1 + x2, max(y1, y2), 0, 0), extents)
358+
# internal element placement
359+ if align in ("baseline", "left", "bottom"):
360+
y_height = 0
361+
elif align in ("right", "top"):
362+
y_height = y2 - y1
363+
elif align == "center":
364+
y_height = (y2 - y1) / 2
365+
# x-offsets, y-offsets
366+
assert_allclose([(0, y_height), (x1, 0)], offset_pairs)
367+
368+
# VPacker
369+
*extents, offset_pairs = vpacker.get_extent_offsets(renderer)
370+
# width, height, xdescent, ydescent
371+
assert_allclose([max(x1, x2), y1 + y2, 0, max(y1, y2)], extents)
372+
# internal element placement
373+
if align in ("baseline", "left", "bottom"):
374+
x_height = 0
375+
elif align in ("right", "top"):
376+
x_height = x2 - x1
377+
elif align == "center":
378+
x_height = (x2 - x1) / 2
379+
# x-offsets, y-offsets
380+
assert_allclose([(x_height, 0), (0, -y2)], offset_pairs)

0 commit comments

Comments
 (0)
0