Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
Implement head resizing (and reversal) for larrow/rarrow/darrow#29998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Implement head resizing (and reversal) for larrow/rarrow/darrow#29998
Uh oh!
There was an error while loading.Please reload this page.
Conversation
… type hinting for LArrow/RArrow/DArrow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thank you for opening your first PR into Matplotlib!
If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.
You can also join uson gitter for real-time discussion.
For details on testing, writing docs, and our review process, please seethe developer guide
We strive to be a welcoming and open project. Please follow ourCode of Conduct.
CharlieThornton33 commentedMay 9, 2025
Hi everyone, I was just wondering whether someone would be willing to have a look at this PR? I feel like it's in a complete or nearly-complete state but need some guidance - the two failing CI jobs seem to be:
Any help would be greatly appreciated. Thanks! |
timhoffm commentedMay 9, 2025
PR-cleanliness: This is a check so that we don't merge commits with unneeded images, which would increase repo size. You can either squash and force-push. Or ignore this and we'll squash-merge at the end. Ignore the macos test failure. It's unrelated. |
CharlieThornton33 commentedMay 9, 2025
Thank you for getting back so quickly! Would it be okay if you handled the squashing - I'm relatively new to git, and I'm not sure of the best method to use for squashing to keep a relatively clean but traceable commit history? |
timhoffm left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Looking at the arrows in the test image,https://github.com/matplotlib/matplotlib/pull/29998/files#diff-ff8adb592a399d60ee5c93fbff7552182f53b74be4573ca21db404ef3c3a6673, the outline sometimes cuts through the text, which must not happen.
From plotting
plt.text(0.5, 0.5, 'text', size=24, bbox=dict(boxstyle="rarrow", fc="w", ec="k"),)plt.text(0.5, 0.5, 'text', size=24, bbox=dict(boxstyle="square", pad=0, fc="C0", ec='none'))we can see that the original calculation had a shaft with the length of the text, i.e. the arrow head starts at -pad from the end of the text.
This setting was ok for the hard-coded parameters, but when head_size and head_angle can vary, we have to invest more into a good positioning of arrow head.
Uh oh!
There was an error while loading.Please reload this page.
CharlieThornton33 commentedMay 10, 2025
Since I'm going to need to make some relatively major changes to the contents of this PR (a redo of the arrow drawing code and a rewrite of its unit test), would it be better for me to amend my last commit (d9b0b32) as suggested in thecontributing guide, or make a few new commits? I'm not quite sure how to add more commits to an open PR - do I just push the changes to my development branch? |
rcomer commentedMay 10, 2025
@CharlieThornton33 yes you can just push more commits to your current branch and they will appear in this PR. Since you already have several commits they will ultimately need squashing one way or the other (and I see you already asked that we handle that), so I do not think you gain anything from the |
CharlieThornton33 commentedMay 14, 2025
I've just redone the padding (only for It's also a bit concerning that the CI workflow seems to run on every commit to this PR - am I okay to disable tests in my commit messages until the one where I write the new test to save on CI resources? |
CharlieThornton33 commentedMay 14, 2025
I've just noticed - all of the |
rcomer commentedMay 14, 2025
@CharlieThornton33 I wouldn't worry about the CI failing, but if they are still running when you push another commit they will be interrupted and re-started. |
timhoffm commentedAug 7, 2025
Given that changes are relatively small and that positioning is happening for the texts, not the box tips of the arrow, I'd claim that the box style is rather decoration and not primarily meant to position the tip of the arrow at a specific coordinate. Therefore, I'd be ok with a breaking change if it opens the route for more functionality. |
CharlieThornton33 commentedAug 9, 2025
There seems to be some issues with text positioning; for example, we have things like I'm not sure why this is happening; using |
CharlieThornton33 commentedSep 8, 2025
@timhoffm I'm sorry I've not been able to come back to this since early August - I've been really busy with work. I was just wondering whether you've seen any positioning issues like this before; I haven't been able to determine what's different with how the arrow's being drawn now that positions the text differently. Any help would be greatly appreciated. |
timhoffm commentedSep 8, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
No worries. It takes as long as it takes. We all have other priorities beyond matplotlib in our lifes. As to the text positioning, no I haven't seen anything like this before. You could try a variant of this to draw a rectangle bounding box in addtion to the text for debugging purposeshttps://github.com/matplotlib/matplotlib/pull/29872/files Interestingly, when you look atboxarrow_adjustment_test_image.png it only happens for darrow, not for larrow and rarrow, so I suspect it's somewhere in the darrow logic not in the text bounding box itself. |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
CharlieThornton33 commentedSep 11, 2025
I'm a bit confused about how to test one of@QuLogic's review notes: How would it be possible to test modification of a public attribute? Would it just be initialising the box with |
timhoffm commentedSep 12, 2025
I believe@QuLogic's comment is fuelled by the idea: What if somebody created a boxstyle class and then modified it arrowstyle=LArrow(head_angle=45)plt.text(0,0,"text",bbox={'boxstyle':arrowstyle}arrowstyle.head_angle=30plt.show() Since the class stores all parameters in attributes as is and all of the logic is in |
| # Pad to position original box and its margins exactly inside arrow shaft | ||
| padding_offset= (0.5*pad)+ (0.25*mutation_size) | ||
| x0-=padding_offset |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
| # Pad to position original box and its margins exactly inside arrow shaft | |
| padding_offset= (0.5*pad)+ (0.25*mutation_size) | |
| x0-=padding_offset | |
| # Pad to position original box and its margins exactly inside arrow shaft | |
| padding_offset= (0.5*pad)+ (0.25*mutation_size) | |
| x0-=padding_offset |
I don't understand this. I thought the original box starts at
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I'm not entirely sure myself why this is needed. When writing the code for padding, I noticed that padding just viaprint(<text object>.get_bbox_patch().get_path()) to look numerically at the positioning of the drawn vertices. From this, I found that there was a discrepancy present that depended only onself.pad linearly, then used linear regression to get this empirical expression forpadding_offset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Onmain, without settinghead_length, etc, the example from the What's new note looks like:
whereas here, it looks like:
and if we revert the extrapadding_offset, it goes closer to before:
but it's still not a perfect match:
So I don't quite understand the math added here, and I guess it depends on how close we want it to match the old version. Currently, it doesn't at all, but withoutpadding_offset, it's still not exactly the same either.
CharlieThornton33 commentedSep 19, 2025
Hi, I was wondering how I should proceed with this PR - I can't think of anything else that needs finishing for it. |
timhoffm commentedSep 20, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
The result looks good. Though, I would like to understand the calculation#29998 (comment). Relying on empirical values that we don’t understand poses a danger that they are incorrect for some cases. We need to debug the logic step by step and either extract the coordinates to draw the picture by hand, or draw points/rectangles (see e.g.#29833 (comment)) to understand what’s going on. |
| ..plot:: | ||
| :include-source: true | ||
| :alt: Six arrow-shaped text boxes, all containing the text "Arrow". The top left arrow has a shorter head than default, while the top right arrow a longer head. The centre left double arrow has a "road-sign" shape (head as wide as the arrow tail), while the centre right arrow has a "backwards" head. The bottom left arrow has two heads which are larger than default, and the bottom right arrow has a head narrower than its tail. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
You should be able to wrap this as long as you wrap the first line.
:alt: Six arrow-shaped text boxes, ... more text ...| # Pad to position original box and its margins exactly inside arrow shaft | ||
| padding_offset= (0.5*pad)+ (0.25*mutation_size) | ||
| x0-=padding_offset |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Onmain, without settinghead_length, etc, the example from the What's new note looks like:
whereas here, it looks like:
and if we revert the extrapadding_offset, it goes closer to before:
but it's still not a perfect match:
So I don't quite understand the math added here, and I guess it depends on how close we want it to match the old version. Currently, it doesn't at all, but withoutpadding_offset, it's still not exactly the same either.
timhoffm commentedSep 23, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
my main concern is consistency. The new code must produce reasonable results, I.e. text must be inside the arrow outline, for all parameter combinations. The code should be not too complex (try to avoid too much special-casing). Matching existing behavior is only third - note that these are outlines around a text, and are defined via the text position. The tip position and arrow size are not explicitly set but depend on font metrics and size. I therefore think it is permissible to not exactly reproduce existing behavior. |
QuLogic commentedSep 23, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
timhoffm commentedSep 23, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
tacaswell commentedSep 25, 2025
Discussed an the call. Keeping consistency on these is not super important, no one should be extracting quantitative information from these arrows.
Personally I think the smart algorithm will probably look better, but fixed pad is my second choice. |
timhoffm commentedSep 26, 2025
Just as a reminder, I've drawn all(?) the options in#29998 (comment),#29998 (comment). The bounding-box options (4), (5) are generally not as nice as thought; or at least not better than the simple ones (1), (2). I believe if we want to be smart, we must be even smarter - possibly a combination of cases or some more advanced distance metric for tip-to-text. Would it be an option to mark the behavior as provisional and allow us the freedom to later change the head positioning algorithm? - Only slight caveat is that we change the behavior for existing users now (one-time break is justified to enable generalization), but we would possibly break them again and leave them in a limbo state in between. Feature flags (#30519) would help with these, but we'd still have to implement them soon. Overall, possibly the sane decision is to defer this topic to after the 3.11 release. |
jklymak commentedSep 26, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
As a meta comment - if the goal is "to bring attention to a certain region on the axes" this doesn't seem like the right tool. I'd make a precisely placed arrow and add text over top and adjust the text to fit the aesthetics of the arrow. Trying to reverse engineer that behaviour into something precisely depending on a text bounding box doesn't seem like a good API. |
timhoffm commentedSep 26, 2025
That is true and the reason why we can do a breaking change in size/positioning. |








Uh oh!
There was an error while loading.Please reload this page.
PR summary
When creating an annotation using boxed text in an arrow shape (
patches.BoxStyle.LArrow/RArrow/DArrow),head_widthandhead_anglearguments may now be passed to adjust the size of the arrow head(s), and (via the use of negative angles) generate 'reversed' arrow heads. Addresses#24618 and is a direct continuation of@Abitamim's work in#24744.This solves both the issue initially mentioned in#24618 (creating a pentagonal 'road-sign' annotation box), as well as allowing for many other customisation options for arrows; for example, a reversed-head
DArrowcould act as a way to bring attention to a certain region on an axis.A new test,
test_boxarrow_adjustment, has been added to test these options.PR checklist