Skip to content

Introduce _TickGroup facade for collective tick operations - #32623

Open
timhoffm wants to merge 2 commits into
matplotlib:mainfrom
timhoffm:tick-group-facade
Open

timhoffm wants to merge 2 commits into
matplotlib:mainfrom
timhoffm:tick-group-facade

Conversation

@timhoffm

Copy link
Copy Markdown
Member

PR summary

Per #5665, tick management is a substantial cost factor with the potential for large performance gains. Changes are complicated because (1) many details of the ticks are publically exposed and (2) tick management is complicated and scattered thoughout axis.py - i.e. we already try to lazily instantiate, copy some state etc.

In #5665 (comment) I outlined a way how we can get out of this mess.

This PR implements steps

  1. create a fascade _TickGroup around the ticks and provide operations to work on all ticks at once and
  2. Replace all internal loops over ticks by these aggregate functions.

While the code change looks substantial, it's actually a pure straight forward refactoring:

  • I have merely moved the code snippets that mess with ticks into dedicated function grouped under the facade _TickGroup; you'll find most sippets moved almost 1:1.
  • _TickGroup does not own tick-related state (yet). Tick storage stays in majorTicks/minorTicks.

Some notable properties:

  • Subclass hooks (get_major_ticks/get_minor_ticks, _get_tick, _copy_tick_props) still work.
  • Speed is unchanged.
  • Left out on purpose: the draw/layout path (_update_ticks returning ticks_to_draw, _get_ticklabel_bboxes, the draw loop). Spines, mplot3d and axisartist use that tick list, so it belongs to the representation switch (step 3).
  • The first commit adds only tests on previously uncovered code paths: They pass on the unchanged code. - These tests are Claude-suggested. I'm only +0.2 on adding them. They are at the border of being too excessive, but I left them because they reasonably increase coverage, which may be a good thing if we want to do larger refactorings.

Why is this reaonable / what's next?

With all tick operations aggregated in TickGroup I plan to investigate optimization potentials (step 3+4 in #5665 (comment)). What exactly is still to be seen, could e.g. be collections, or "lightweight" Artists with shared properties or just lazily created Artists (each tick consists of 3 Line2D and 2 Text objects and a substantial part of them is often unused).

But even if we don't optimize, this refactoring is already a win. The tick management responsibility was spread throughout the whole Axis class (and even beyond - see the backend_bases change here). Collecting them in one class and giving the operations clear names substantially improves architecture and ability to reason about the code.

AI Disclosure

Claude Code (Claude Opus 5.5) was used to profile tick creation, to look through the existing per-tick code paths, and to draft the _TickGroup implementation and the new tests, based on the design I outlined in #5665. I reviewed and adjusted the code.

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New features and API changes have release notes
  • Documentation complies with general and docstring guidelines

timhoffm and others added 2 commits November 19, 2026 00:27
Add tests for previously untested functionality that operates on all
ticks of an Axis: grid toggling via key presses,
`Axis.get_ticks_direction`, `Axis.get_tick_padding`, and the label
alignment when dragging polar r-labels.

These pass with the existing implementation and serve as a safety net
for the subsequent refactoring of tick handling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add an internal `_TickGroup` class (one per Axis for major and minor
ticks) that bundles all operations on the set of ticks, and route
internal code through it instead of looping over individual `Tick`
objects. This isolates the internal usage from the tick representation
(currently lists of `Tick`), as a first step towards a more efficient
collection-based implementation; see matplotlib#5665.

This is a pure refactoring without behavior change. The tick lists are
still stored in `Axis.majorTicks`/`Axis.minorTicks`, and subclass hooks
(`get_major_ticks`, `get_minor_ticks`, `_get_tick`, `_copy_tick_props`)
are still honored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant