Skip to content

FancyArrowPatch picker fails depending on arrowstyle #8384

Closed

Description

@lejar

Bug report

Bug summary

The picker of FancyArrowPatch will not work with some arrow styles (see included example). Certain arrow styles will cause the picker to produce a pick event if the user clicks anywhere to the left of the arrow.

Code for reproduction

from matplotlib import pyplot as plt
from matplotlib import patches


def pick_event(event):
    print("PICK", event.artist)


def main():
    fig = plt.figure()
    axes = fig.add_subplot(111)
    axes.set_xlim((0, 10))
    axes.set_ylim((0, 10))

    arrow = patches.FancyArrowPatch(
        (8, 2),
        (9, 3),
        # Uncommenting this will make the pick event be triggered every time I
        # click to the left of the arrow.
        # arrowstyle='-|>',
    )
    # Same thing happens with arrow.set_picker(True)
    arrow.set_picker(4)

    fig.canvas.mpl_connect('pick_event', pick_event)
    axes.add_patch(arrow)
    plt.show()


if __name__ == '__main__':
    main()

Actual outcome
(With the arrowstyle line uncommented)
A pick event is always triggered when clicking to the left of the arrow.

PICK FancyArrowPatch(8,2->9,3)
PICK FancyArrowPatch(8,2->9,3)
PICK FancyArrowPatch(8,2->9,3)
PICK FancyArrowPatch(8,2->9,3)
PICK FancyArrowPatch(8,2->9,3)

Expected outcome
The pick event is not triggered unless the user actually clicks on the arrow.

Matplotlib version
I tested this with two versions of matplotlib:

  • version 2.0.0 installed via pip 9.0.1 using Python 2.7.12 included in ubuntu
  • version 1.5.3 built from source using Python 2.7.11 built from source

OS information:

$ uname -a
Linux 4.4.0-62-generic #83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

Activity

  1. added this to the milestone on Mar 29, 2017
  2. phobson commented on Mar 29, 2017

    @phobson
    Member

    @lejar thanks for the detailed report and sorry for the slow response. hopefully one of us will be able to take a closer look at this soon.

  3. modified the milestones: , on Mar 30, 2017
  4. tacaswell commented on Mar 30, 2017

    @tacaswell
    Member

    Something is clearly wrong with the hit testing...

  5. tacaswell commented on Mar 30, 2017

    @tacaswell
    Member
    from matplotlib import pyplot as plt
    from matplotlib import patches
    
    picks = []
    
    def pick_event(event):
        picks.append(event)
        print("PICK", event.artist, event.mouseevent)
    
    
    
    fig = plt.figure()
    axes = fig.add_subplot(111)
    axes.set_xlim((0, 10))
    axes.set_ylim((0, 10))
    
    arrow = patches.FancyArrowPatch(
        (8, 2),
        (9, 3),
        # Uncommenting this will make the pick event be triggered every time I
        # click to the left of the arrow.
        # arrowstyle='->',
    )
    
    arrow2 = patches.FancyArrowPatch(
        (8, 7),
        (9, 8),
        # Uncommenting this will make the pick event be triggered every time I
        # click to the left of the arrow.
        arrowstyle='->',
    )
    # Same thing happens with arrow.set_picker(True)
    arrow.set_picker(4)
    arrow2.set_picker(4)
    
    fig.canvas.mpl_connect('pick_event', pick_event)
    axes.add_patch(arrow)
    axes.add_patch(arrow2)
    plt.show()
    
    
    def hit_mask(ax, paths):
        N, M = 200, 200
        X, Y = np.meshgrid(np.linspace(*ax.get_xlim(), N),
                           np.linspace(*ax.get_ylim(), M))
        res = np.zeros((N, M))
        for k, (x, y) in enumerate(zip(X.ravel(),
                                            Y.ravel())):
            for m, p in enumerate(paths):
                if p.contains_point((x, y), None, .1):
                    res[np.unravel_index(k, (N, M))] += 2**m
        im = ax.imshow(res, extent=(X.min(), X.max(), Y.min(), Y.max()),
                  origin='lower', cmap='Set3')
        im.get_cmap().set_under('w')
        im.set_clim(vmin=.5)
    
    hit_mask(axes, [arrow.get_path(), arrow2.get_path()])

    so

    Sorry the code is messy, this is just what I have been playing with trying to debug this.

  6. QuLogic commented on Mar 31, 2017

    @QuLogic
    Member

    I think the second path is not closed?

    (Pdb) p arrow.get_path()
    Path(array([[ 8.03108293,  2.03445713],
           [ 8.49475998,  2.49813417],
           [ 8.95843702,  2.96181121],
           [ 8.95663002,  2.96506551],
           [ 8.96216088,  2.96637862],
           [ 8.96769173,  2.96769173],
           [ 8.96517254,  2.96095479],
           [ 8.96265335,  2.95421786],
           [ 8.96084635,  2.95747215],
           [ 8.49716931,  2.49379511],
           [ 8.03349226,  2.03011807],
           [ 8.03108293,  2.03445713],
           [ 8.03108293,  2.03445713]]), array([ 1,  3,  3,  2,  3,  3,  3,  3,  2,  3,  3,  2, 79], dtype=uint8))
    

    vs.

    (Pdb) p arrow2.get_path()
    Path(array([[ 8.0322876 ,  7.0322876 ],
           [ 8.49998966,  7.49998966],
           [ 8.94961703,  7.94961703],
           [ 8.9407411 ,  7.94748949],
           [ 8.94961703,  7.94961703],
           [ 8.94555976,  7.93881137]]), array([1, 3, 3, 1, 2, 2], dtype=uint8))
    

    Note the missing 79/CLOSEPOLY to close the path and the last point doesn't really seem to match the first.

  7. modified the milestones: , on May 3, 2017
  8. colonelsammy commented on May 10, 2017

    @colonelsammy

    I have the same problem (Windows, built from source at 7d6b0c8).

    Following your example:

    from matplotlib import pyplot as plt
    from matplotlib import patches
    import numpy as np
    
    picks = []
    
    def pick_event(event):
        picks.append(event)
        print("PICK", event.artist, event.mouseevent)
    
    fig = plt.figure()
    axes = fig.add_subplot(111)
    axes.set_xlim((0, 10))
    axes.set_ylim((0, 10))
    
    arrow = patches.FancyArrowPatch(
        (8, 2),
        (9, 3),
        # Uncommenting this will make the pick event be triggered every time I
        # click to the left of the arrow.
        # arrowstyle='->',
    )
    
    arrow2 = patches.FancyArrowPatch(
        (8, 7),
        (9, 8),
        # Uncommenting this will make the pick event be triggered every time I
        # click to the left of the arrow.
        arrowstyle='->',
    )
    # Same thing happens with arrow.set_picker(True)
    arrow.set_picker(4)
    arrow2.set_picker(4)
    
    p = arrow.get_path()
    patch = patches.PathPatch(p, facecolor='orange', lw=2)
    axes.add_patch(patch)
    
    p = arrow2.get_path()
    patch = patches.PathPatch(p, facecolor='orange', lw=2)
    axes.add_patch(patch)
    
    fig.canvas.mpl_connect('pick_event', pick_event)
    axes.add_patch(arrow)
    axes.add_patch(arrow2)
    plt.show()

    image

    That second patch really doesn't look right!

  9. dstansby commented on Jul 29, 2017

    @dstansby
    Member

    Perhaps the problem is that trying to pick an open patch (such as an open arrow) doesn't make sense? (if a pick is defined as clicking inside the patch)

  10. modified the milestones: , on Oct 9, 2017
  11. cacti77 commented on Jul 23, 2018

    @cacti77

    Does anyone know when, or in which version of Matplotlib, this is likely to be fixed please? This issue is still in v2.2.2 on Windows with the arrowstyle values I really want to use, '->' or '-|>'.
    My end-user doesn't like 'wedge', 'fancy' or 'simple'. If this issue isn't going to be fixed for a long time I might write my own picker function for use with '->' or '-|>'.

  12. anntzer commented on Sep 28, 2019

    @anntzer
    Contributor

    AFAICT this was fixed by #10776.

  13. modified the milestones: needs sorting, v3.1.0 on Sep 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions