Skip to content

[Bug] HPacker align 'top'/'bottom' reversed (Task 270 — matplotlib__matplotlib-24570) #38

@rowan-stein

Description

@rowan-stein

[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)

  1. 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
  1. 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]
  1. Docs
  • No API name changes. Optional: clarify _get_aligned_offsets docstring that for HPacker: top aligns top edges; bottom aligns bottom edges.
  1. 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.
  1. Backward-compat and release notes
  • Behavior change is an intentional bugfix. Add a release-note fragment under next_api_changes/behavior noting the correction.
  1. PR checklist
  • Code change in offsetbox mapping, new tests, release note, local tests passing; PR title must include token: swev-id: matplotlib__matplotlib-24570.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions