Skip to content

Fit the legend and title hypertools adds to a caller's axes (1.1.1) - #294

Merged
jeremymanning merged 5 commits into
masterfrom
fix/1.1.1-caller-figure-fit
Nov 22, 2026
Merged

jeremymanning merged 5 commits into
masterfrom
fix/1.1.1-caller-figure-fit

Conversation

@jeremymanning

Copy link
Copy Markdown
Member

Follow-up to #293, for the same unreleased 1.1.1.

What this changes

After #293, hyp.plot(..., ax=ax) no longer lays out or resizes a figure the caller built. That left the legend hypertools hangs off the right of ax running off the canvas, or over the next panel, until the caller laid the figure out; a 3-D legend stayed clipped even after the caller's own fig.tight_layout().

hyp.plot now installs a matplotlib axes locator (_CallerSlotLocator) on a caller-supplied axes that carries a legend or a title. When the figure is drawn it narrows that axes from the right until its legend ends before the canvas edge or the next panel (its visible tick and axis labels, or a colorbar, included), and lowers it until its title is on the canvas. The figure's size, its layout engine and the slot of every other axes are not changed.

  • Recomputed on each draw, so it follows tight_layout, subplots_adjust, a resize, a save at another dpi, or an edit to the legend.
  • While tight_layout or a layout engine is measuring, the locator answers with the unfitted position (and the legend outside), so slots are identical, draw for draw, to the same figure without the locator.
  • No room beside the axes even at 30% of its width: the legend is drawn inside the axes, framed, and goes back out when room appears. A legend the caller restyles after that is left alone.
  • Too large for the panel either way, or running off the top/bottom or over another row: left in place with one UserWarning.
  • Twin axes follow the fitted axes, whenever they are created; legends and titles on twins are counted.
  • Nothing is installed on an axes without a legend or title.

Figures hypertools creates itself (plain hyp.plot, hyp.subplots, panels=) are unchanged: pixel-identical to master in three independent checks.

Measurements

  • Before (master), 18 caller-figure cases in 2-D and 3-D: legend 77-156 px off the canvas or over the next panel in every case without a layout call; 22-25 px off after tight_layout in 3-D. After: none.
  • Redraw of a 4x4 grid with a legend and title on every panel: 163 ms (2-D) and 230 ms (3-D), against 60 and 69 ms with the positions fixed.

Not covered

  • An inset or other axes placed by hand where the legend goes.
  • A legend over the tick labels of its own axes' twin (same on master).
  • On an axes with the locator, ax.get_position() reports the position of the last draw.

Review and tests

Three review rounds (Codex CLI each round, a fresh subagent in rounds 1 and 2); every confirmed finding has a regression test. tests/test_caller_figure_hygiene.py now has 100 tests and passes on matplotlib 3.10.8 and 3.11.2. Full suite: 6904 passed, 1 failed (missing docstrings on the new helpers, fixed in the last commit and re-run with the 24 test files that draw into caller axes). Sphinx -W build with every gallery example re-executed succeeded. Session log: notes/session_2026-11-21_release-1.1.1.md.

🤖 Generated with Claude Code

https://claude.ai/code/session_01DcKtysUSGLqA8RApYP9xYG

jeremymanning and others added 5 commits November 21, 2026 19:29
A legend beside a caller-supplied axes ran off the canvas or over the next
panel until the caller laid the figure out, and a 3-D legend stayed clipped
even after the caller's tight_layout. An axes locator now narrows only that
axes, at draw time and inside the room the caller's layout gave it, until
its legend and title fit; the figure's size, engine and other axes are
untouched.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtysUSGLqA8RApYP9xYG
Recompute the fit on every draw (no memo), answer layout passes with the
unfitted position, make twins follow, treat colorbars and visible neighbor
labels as limits, keep titles on their side, restore an inside legend when
room appears, and report legends that cannot be seated.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtysUSGLqA8RApYP9xYG
Restore data limits around the search, report legends that run over rows,
cut the per-draw cost, measure layouts with the legend outside, follow
twins made later, leave a restyled legend to the caller, recognize layout
passes by module, and read neighbor labels where the next draw puts them.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtysUSGLqA8RApYP9xYG
Check the legend's vertical room on the final box, fit titles on twins,
release a twin whose leader was removed, and treat a recolored inside
legend as the caller's.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DcKtysUSGLqA8RApYP9xYG
@jeremymanning
jeremymanning merged commit e3abb84 into master Nov 22, 2026
18 checks passed
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.

1 participant