Skip to content

swev-id: matplotlib__matplotlib-24570 fix HPacker top/bottom alignment#41

Open
casey-brooks wants to merge 1 commit intomatplotlib__matplotlib-24570from
fix/hpacker-align-reversed-24570
Open

swev-id: matplotlib__matplotlib-24570 fix HPacker top/bottom alignment#41
casey-brooks wants to merge 1 commit intomatplotlib__matplotlib-24570from
fix/hpacker-align-reversed-24570

Conversation

@casey-brooks
Copy link

Reproduction steps

  1. Use the script from [Bug] HPacker align 'top'/'bottom' reversed (Task 270 — matplotlib__matplotlib-24570) #38 (reproduced below) and set align="bottom" or align="top" on the HPacker.
import matplotlib.pyplot as plt
from matplotlib.offsetbox import DrawingArea, HPacker, VPacker, AnchoredOffsetbox, TextArea
from matplotlib.patches import Rectangle


da1 = DrawingArea(10, 20)
rect1 = Rectangle((0, 0), 10, 20)
da1.add_artist(rect1)

da2 = DrawingArea(10, 30)
rect2 = Rectangle((0, 0), 10, 30)
da2.add_artist(rect2)

align = "bottom"

pack = HPacker(children=[da1, da2], pad=10, sep=10, align=align)
title = TextArea(f"align='{align}'")
pack = VPacker(children=[title, pack], sep=10, pad=10, align="center")

box = AnchoredOffsetbox(child=pack, loc="center")

_, ax = plt.subplots()
ax.add_artist(box)

Observed failure

  • align='bottom' aligns the top edges and align='top' aligns the bottom edges, so the visual result is inverted relative to the API contract.

Expected behavior

  • align='top' should flush the children's top edges and align='bottom' should flush their bottom edges.

Summary of changes

  • Swap the HPacker top/bottom mapping in _get_aligned_offsets so offsets make top alignment flush the top edges and bottom alignment flush the bottom edges.
  • Clarify the HPacker docstring and add geometry-based regression tests that cover top, bottom, center, and baseline alignment along with a VPacker left/right sanity check.
  • Add a behavior release note fragment describing the bugfix.

Testing

  • pytest -k offsetbox
  • flake8 lib/matplotlib/offsetbox.py lib/matplotlib/tests/test_offsetbox_alignment.py

Fixes #38.

@casey-brooks casey-brooks requested a review from a team December 25, 2025 09:49
@casey-brooks
Copy link
Author

Tests & Lint Summary

  • pytest -k offsetbox (276 passed, 8 skipped)
  • flake8 lib/matplotlib/offsetbox.py lib/matplotlib/tests/test_offsetbox_alignment.py (no issues)

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swapping the HPacker top/bottom handling fixes the inversion while leaving center and baseline math unchanged. Verified VPacker offsets are stable and the new geometry-based tests cover top/bottom/center/baseline as well as a left/right sanity check. Release note fragment is in the right location with clear wording. Looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants