Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to update position of arrow patch #27692

Merged
merged 1 commit into from
Feb 2, 2024

Conversation

saranti
Copy link
Contributor

@saranti saranti commented Jan 24, 2024

PR summary

Closes #14217. Adds set_data method from FancyArrow that allows the arrow to be repositioned without re-drawing.
Arrow's x, y, dx, dy and width parameters are now stored in private variables so that they can be reused for recreating the transform.

e.g.

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Arrow

fig, ax = plt.subplots()
ax.plot([0, 4], [10, 0])

a = mpl.patches.Arrow(2, 0, 0, 10)
ax.add_patch(a)

# code for modifing the arrow
a.set_data(x=.5, y=0, dx=3, dy=8, width=1.2)

plt.show()

See the discussion here #14217 (comment). I don't know what the consensus is on the Arrow class but If this fix is still needed, I'll add a test and a what's new entry.

PR checklist

@timhoffm
Copy link
Member

While the arrow patch has fundamental flaws, it's not going to go away soon. So this addition is reasonable.

Copy link
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

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

Test please? sorry should read things better :/

lib/matplotlib/patches.py Outdated Show resolved Hide resolved
ax.add_patch(a)

# code for modifying the arrow
a.set_data(x=.5, dx=3, dy=8, width=1.2)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
a.set_data(x=.5, dx=3, dy=8, width=1.2)
def update(i):
a.set_data(x=.5, dx=3, dy=8, width=1.2)

how would you feel about making this a tiny animation to advertise the purpose of the method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sounds good. I'll have a go at making an animation

lib/matplotlib/patches.py Outdated Show resolved Hide resolved
@saranti saranti marked this pull request as draft January 29, 2024 07:21
@saranti saranti marked this pull request as ready for review January 29, 2024 09:06
Comment on lines +1315 to +1319
x, y : float or None, default: None
The x and y coordinates of the arrow base.

dx, dy : float or None, default: None
The length of the arrow along x and y direction.
Copy link
Member

Choose a reason for hiding this comment

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

Is tedious but I think these need to be documented individually since they come in individually and not as tuples.

Copy link
Member

@timhoffm timhoffm Jan 29, 2024

Choose a reason for hiding this comment

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

This is correct as is. numpydoc explicitly allows this:

When two or more input parameters have exactly the same type, shape and description, they can be combined:

@story645 story645 added this to the v3.9.0 milestone Jan 29, 2024
@dstansby dstansby merged commit 61661ab into matplotlib:main Feb 2, 2024
38 of 42 checks passed
@saranti saranti deleted the update_arrow branch February 3, 2024 06:25
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.

[Feature request] Add a way to update the position of the Arrow patch.
4 participants