Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,3 +1236,19 @@ def test_text_tightbbox_outside_scale_domain():
invalid_text = ax.text(0, -5, 'invalid')
invalid_bbox = invalid_text.get_tightbbox(fig.canvas.get_renderer())
assert not np.isfinite(invalid_bbox.width)


def test_annotation_patchA_not_overridden():
# Test that manually setting patchA on annotation arrow is not
# overridden by update_positions. See GitHub issue #28316.
fig, ax = plt.subplots()
ann = ax.annotate(
'',
xy=(0.5, 0.6),
xytext=(0.2, 0.5),
arrowprops=dict(arrowstyle="->")
)
text = ax.text(0.2, 0.5, 'Text', ha='center', va='center')
ann.arrow_patch.set_patchA(text)
fig.draw_without_rendering()
assert ann.arrow_patch.patchA is text
5 changes: 4 additions & 1 deletion lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -2101,7 +2101,10 @@ def update_positions(self, renderer):
xy=(bbox.x0 - pad / 2, bbox.y0 - pad / 2),
width=bbox.width + pad, height=bbox.height + pad,
transform=IdentityTransform(), clip_on=False)
self.arrow_patch.set_patchA(patchA)
if self.arrow_patch.patchA is getattr(
self, '_internal_patchA', None):
self.arrow_patch.set_patchA(patchA)
self._internal_patchA = patchA

Comment on lines +2104 to 2108

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not make sense to me. Please explain what this does and why it is needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sir,
The issue is that update_positions() runs on every draw and always overwrites patchA with a recomputed value - so if a user manually calls set_patchA(), their value gets silently lost on the next draw.

The fix: I store the last patchA I set internally in _internal_patchA. Before overwriting, I check if arrow_patch.patchA is still the same object. If yes then user hasn't touched it, safe to update. If no then user set it manually, so I skip the override.

getattr(self, '_internal_patchA', None) handles the first draw gracefully- defaults to None so the initial set always works fine.

Added test_annotation_patchA_not_overridden to verify this- it manually sets a custom patchA, triggers draw_without_rendering(), and asserts the value is preserved after the draw.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I would question if manually setting patchA is a valid use case. Annotation is primarily a Text object with the ability to refer to a coordinate position xy. It can optionally draw an arrow from its xytext postion to xy. That arrow (starting at xytext) may be partially covered by the text and the text bounding box serves as patchA. I would say that the patchA is owned by the Annotation and I am not clear that a user-override is reasonable.

It may be that the use case should instead be solved via ConnectionPatch, see #26440. @story645 do you agree?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sir,
The original use case (from @Phlya) is in the adjustText library, where ax.annotate() is used instead of FancyArrowPatch directly — and the user needs to modify arrow_patch.patchA afterward to avoid arrows passing through text objects.

If ConnectionPatch is the recommended approach for this use case, I'm happy to close this PR. But if set_patchA() is documented as a valid method on arrow_patch, silently ignoring it seems like a bug worth fixing.

Happy to follow your guidance on the right direction!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unclear why the original user wants to overwrite the patchA - #28316 (comment).

Let's first try to find out what the actual problem is that the user wants to solve.

@artist.allow_rasterization
def draw(self, renderer):
Expand Down
Loading