Skip to content

[Feature] Text alignment#754

Open
cvanelteren wants to merge 3 commits into
Ultraplot:mainfrom
cvanelteren:feature-textalign
Open

[Feature] Text alignment#754
cvanelteren wants to merge 3 commits into
Ultraplot:mainfrom
cvanelteren:feature-textalign

Conversation

@cvanelteren

Copy link
Copy Markdown
Collaborator

Closes #753 for text alignment

I implemented a KD tree approach to determine the closest neighbor between text elements to prevent overlap. How this works is that by default it would keep track of texts and aligns them on the final draw. Nothing is registered unless you pass avoid_overlap=True (or flip rc['text.align']), so the solver never even imports on an ordinary figure. The naive approach would be to do this with O(n^2) pairwise comparison but that scales poorly and is not necessary.

A plot with 30 labeled texts with run in about 80ms compared to 50ms natively without any draws. This approach will scale not properly for large number of labelled elements (say > 500) but I don't think that will ever occur for plots as it will be unreadable anyway.

@codecov

codecov Bot commented Jul 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.16909% with 29 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
ultraplot/textalign.py 91.50% 16 Missing and 10 partials ⚠️
ultraplot/tests/test_textalign.py 99.50% 1 Missing and 1 partial ⚠️
ultraplot/axes/base.py 97.82% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@cvanelteren

cvanelteren commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator Author

TODO: add tests

@cvanelteren cvanelteren self-assigned this Jul 13, 2026
@cvanelteren cvanelteren requested a review from gepcel July 13, 2026 07:17
@cvanelteren cvanelteren marked this pull request as ready for review July 13, 2026 07:17
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.

Overlapping elements

1 participant