Skip to content

Add IndirectTransform for lazy coordinate resolution#31488

Open
dikshajangra12918-oss wants to merge 10 commits intomatplotlib:mainfrom
dikshajangra12918-oss:indirect-transform-v2
Open

Add IndirectTransform for lazy coordinate resolution#31488
dikshajangra12918-oss wants to merge 10 commits intomatplotlib:mainfrom
dikshajangra12918-oss:indirect-transform-v2

Conversation

@dikshajangra12918-oss
Copy link
Copy Markdown
Contributor

PR Summary

While exploring issue #22223, I noticed that the coordinate dispatch logic in '_get_xy_transform()' is tightly coupled inside '_AnnotationBase' class in 'text.py'.

This PR adds 'IndirectTransform' - a new class in 'transforms.py' that wraps a callable and resolves it lazily at draw time.

##How this fixes #22223
Currently, there is no clean way to position an artist relative to multiple other artists combined. The problem is that bounding boxes of artists are not known untill draw time.

'IndirectTransform' solves this by storing a callable (like a lambda) that gets called at draw time - when bounding boxes are finally available. This means you can now do:

tr = IndirectTransform(
    lambda: Bbox.union([
        txt1.get_window_extent(),
        txt2.get_window_extent()
    ])
)
'''
This allows any artist to be positioned relative to the combined bounding box of multiple other artists - which is exactly what #22223 requests.

## What changed
Added IndirectTransform class in lib/matplotlib/transforms.py

## Testing
import matplotlib.transforms as T
bbox = T.Bbox([[0, 0], [1, 1]])
tr = T.IndirectTransform(lambda: bbox)
print(tr._resolve())  # BboxTransformTo(Bbox(...))

## AI Assistance
1. Understand the existing transfrom hierarchy in 'transforms.py'
2. Trace through the 6 cases in '_get_xy_transform()'
3. Draft and refine the initial implementation

dikshajangra12918-oss and others added 8 commits April 2, 2026 14:22
Setting patchA on Annotation.arrow_patch has no visible effect because the arrow position is not recomputed.

This change ensures that the annotation is marked as stale when patchA changes, triggering proper recomputation during rendering.
Add logic to prevent overriding user-set patchA in update_positions. Fixes issues matplotlib#28316.
Add test to verify that manually set patchA on  annotation arrow is preserved after draw.
1.  Add 2 blank lines before function definition
2.  Split long comment line to stay within 80 chars
Only update patchA internally if user has not manually set it.
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.

[ENH]: Position artists (particularly Annotations) relative to multiple other artists

1 participant