Skip to content

Annotation.contains and FancyArrow.contains return incorrect values #10875

Closed

Description

@anntzer

Bug report

Consider the following example:

from pylab import *
from matplotlib.backend_bases import MouseEvent
rcdefaults()

fig, axs = plt.subplots(2)
for i, ax in enumerate(axs):
    ax.set(xlim=(0, 1), ylim=(0, 1))
    ann = ax.annotate(
        "hello", xy=(.4, .4), xytext=(.6, .6), arrowprops={"arrowstyle": "->"})
    ax.figure.canvas.draw()
    xs = np.linspace(*ax.get_xlim(), 30)
    ys = np.linspace(*ax.get_ylim(), 30)
    for x in xs:
        for y in ys:
            event = MouseEvent(
                "button_press_event",
                ax.figure.canvas,
                *ax.transData.transform_point((x, y)))
            art = [ann, ann.arrow_patch][i]
            ax.plot(x, y, ".", c="g" if art.contains(event)[0] else "r")
plt.show()

figure_1

Points in green mark (a) in the top plot, points that are "contained" (per artist.contains) in the annotation object; (b) in the bottom plot, points that are "contained" in the arrow patch of the annotation object.

Obviously neither set of contained points is correct. This affects, for example, the points that trigger interactive dragging of the annotation.

Matplotlib version

  • Operating system:
  • Matplotlib version: 2.2.1, but also present in 1.5
  • Matplotlib backend (print(matplotlib.get_backend())): qt5
  • Python version: 3.6
  • Jupyter version (if applicable):
  • Other libraries:

Activity

  1. QuLogic commented on Mar 25, 2018

    @QuLogic
    Member

    (Partial) duplicate of #8384; maybe fixed by #10776?

  2. anntzer commented on Mar 25, 2018

    @anntzer
    ContributorAuthor

    #10776 fixes the containment test for the arrow, but not for the annotation (in that case, the whole bbox of {text + arrow} is selected).

  3. added this to the milestone on Mar 26, 2018
  4. TarasKuzyo commented on Jul 3, 2018

    @TarasKuzyo
    Contributor

    In the case of the annotation Text.contains(self, event)
    in Annotation. contains returns correct values,

    def contains(self, event):
    contains, tinfo = Text.contains(self, event)
    if self.arrow_patch is not None:
    in_patch, _ = self.arrow_patch.contains(event)
    contains = contains or in_patch
    return contains, tinfo

    so fixing containment for the arrow
    should automatically fix it for the annotation.

  5. modified the milestones: v3.0, v3.1 on Aug 11, 2018
  6. modified the milestones: v3.1.0, v3.2.0 on Mar 18, 2019
  7. dstansby commented on Apr 22, 2019

    @dstansby
    Member

    Looks like this is fixed on master (using the example code above):

    Figure_1

  8. modified the milestones: v3.2.0, v3.1.0 on Apr 22, 2019
  9. tacaswell commented on Apr 22, 2019

    @tacaswell
    Member

    Also fixed on v3.1.x

  10. timhoffm commented on May 4, 2019

    @timhoffm
    Member

    This is not yet fixed. It should look like this

    grafik

    Which will be fixed by #10776 as mentioned above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions