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

[Bug]: ShrinkA and ShrinkB are ignored in ax.annotate(arrowprops=...) #27941

Closed
thehappycheese opened this issue Mar 18, 2024 · 3 comments · Fixed by #27947
Closed

[Bug]: ShrinkA and ShrinkB are ignored in ax.annotate(arrowprops=...) #27941

thehappycheese opened this issue Mar 18, 2024 · 3 comments · Fixed by #27947
Milestone

Comments

@thehappycheese
Copy link
Contributor

thehappycheese commented Mar 18, 2024

Bug summary

Despite being documented [here] and (https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.annotate.html)
here, providing ShinkA and ShrinkB are ignored in annotations.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
x = np.linspace(0, 10, 100)
y = np.sin(x)
ax.plot(x, y)

# Annotate a some points with varying shrinkA values
annotation_x_values    = [3,4,5,6,7]
shrinkA_values = [0, 0.2, 0.4, 0.6, 0.8]
for shrinkA, x_anno in zip(shrinkA_values, annotation_x_values):
    y_anno = np.sin(x_anno)
    ax.annotate(
        f'shrinkA={shrinkA}',
        xy=(x_anno, y_anno),
        xytext=(x_anno+1, y_anno+0.2),
        arrowprops=dict(arrowstyle="->", shrinkA=shrinkA),
        fontsize=10
    )


plt.tight_layout()
plt.show()

Actual outcome

All the arrows in the example above look the same; ShinkA/ShrinkB has no effect.
image

Expected outcome

Expected that the beginning / end of the arrows are moved toward or away from the source/target of the arrow.

By way of example, the simplearrow shrink= parameter (when arrowstyle is omitted) works as expected:

Click here to expand code to reproduce the image below
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(figsize=(5, 3))
x = np.linspace(0, 10, 100)
y = np.sin(x)
ax.plot(x, y)

# Annotate a some points with varying shrinkA values
annotation_x_values    = [3,4,5,6,7]
shrink_values = [0, 0.2, 0.4, 0.6, 0.8]
for shrink, x_anno in zip(shrink_values, annotation_x_values):
    y_anno = np.sin(x_anno)
    ax.annotate(
        f'shrink={shrink}',
        xy=(x_anno, y_anno),
        xytext=(x_anno+1, y_anno+0.2),
        arrowprops=dict(shrink=shrink),
        fontsize=10
    )
plt.tight_layout()
plt.show()

image

However then I cant use the desired arrowstyle='-->' or control which end of the arrow is shrunk.

Additional information

I have not been able to confirm, but I belive this comment from the documentation for FancyArrowPatch is the likely cause (link to doc source).

Alternatively if *path* is provided, an arrow is drawn along this path and *patchA*, *patchB*, *shrinkA*, and *shrinkB* are ignored.

My guess was that in the implementation of Axes.annotate, the *path* is provided?
I cant find if that is happening though.

I think debugging near this use set_positions() might be a good starting point?

Operating system

windows

Matplotlib Version

3.8.3

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

Python 3.11.5

Jupyter version

VS Code Notebook (ipython Version: 8.17.2)

Installation

pip

@thehappycheese
Copy link
Contributor Author

thehappycheese commented Mar 18, 2024

Ah doh ok so it turns out that it was working all along... its just that ShrinkA and ShrinkB are not fractional, they are in points.

The following code works as expected

shrinkB_values = np.array([0, 0.2, 0.4, 0.6, 0.8])*30
for shrinkB, x_anno in zip(shrinkB_values, annotation_x_values):
    y_anno = np.sin(x_anno)
    ax.annotate(
        f'shrinkB={shrinkB}',
        xy=(x_anno, y_anno),
        xytext=(x_anno+1, y_anno+0.2),
        arrowprops=dict(arrowstyle="->", shrinkB=shrinkB),
        fontsize=10
    )

In the documentation it notes that the default value is 2 points

@rcomer
Copy link
Member

rcomer commented Mar 18, 2024

Thanks for sorting that out @thehappycheese. I'm wondering if we could make this more obvious by updating the FancyArrowPatch docstring so it also mentions that these parameters are in points.

@thehappycheese
Copy link
Contributor Author

Thanks for sorting that out @thehappycheese. I'm wondering if we could make this more obvious by updating the FancyArrowPatch docstring so it also mentions that these parameters are in points.

Hi Ruth, thanks for taking the time to review! No worries :)

I will create a PR to change this line in the docs

shrinkA, shrinkB : float, default: 2
    Shrinking factor of the tail and head of the arrow respectively.

The word "factor" is misleading and could be replaced with The shrink amount, in points, of the...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants