forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
bugSomething isn't workingSomething isn't working
Description
[Bug]: align in HPacker is reversed
Bug summary
For the align parameter in HPacker, the options top and bottom seem reversed.
Code for reproduction
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)Actual outcome
See screenshots in original report; align='bottom' behaves as top and vice versa.
Expected outcome
Correct semantics: align='top' should align top edges of children; align='bottom' should align bottom edges.
Researcher specification (Emerson Gray)
- Location of the bug
- File: lib/matplotlib/offsetbox.py
- Function: _get_aligned_offsets(hd_list, height, align="baseline")
- Current mapping:
- elif align in ["left", "top"]: offsets = [d for h, d in hd_list]
- elif align in ["right", "bottom"]: offsets = [height - h + d for h, d in hd_list]
- Where HPacker uses it:
- lib/matplotlib/offsetbox.py, class HPacker, method get_extent_offsets
- Fix approach
- Treat as a bugfix: swap top/bottom in the mapping so that:
- top -> offsets = [height - h + d]
- bottom -> offsets = [d]
- Proposed change:
- elif align in ["left", "bottom"]: offsets = [d for h, d in hd_list]
- elif align in ["right", "top"]: offsets = [height - h + d for h, d in hd_list]
- Docs
- No API name changes. Optional: clarify _get_aligned_offsets docstring that for HPacker: top aligns top edges; bottom aligns bottom edges.
- Tests
- Add geometry-based tests (no image baselines) verifying HPacker alignment for top, bottom, center, baseline with different child heights and descents; confirm VPacker left/right remain correct.
- Backward-compat and release notes
- Behavior change is an intentional bugfix. Add a release-note fragment under next_api_changes/behavior noting the correction.
- PR checklist
- Code change in offsetbox mapping, new tests, release note, local tests passing; PR title must include token: swev-id: matplotlib__matplotlib-24570.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working