{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Pick event demo\n\nYou can enable picking by setting the \"picker\" property of an artist\n(for example, a Matplotlib Line2D, Text, Patch, Polygon, AxesImage,\netc.)\n\nThere are a variety of meanings of the picker property:\n\n* *None* - picking is disabled for this artist (default)\n\n* bool - if *True* then picking will be enabled and the artist will fire a pick\n event if the mouse event is over the artist.\n\n Setting ``pickradius`` will add an epsilon tolerance in points and the artist\n will fire off an event if its data is within epsilon of the mouse event. For\n some artists like lines and patch collections, the artist may provide\n additional data to the pick event that is generated, for example, the indices\n of the data within epsilon of the pick event\n\n* function - if picker is callable, it is a user supplied function which\n determines whether the artist is hit by the mouse event. ::\n\n hit, props = picker(artist, mouseevent)\n\n to determine the hit test. If the mouse event is over the artist, return\n hit=True and props is a dictionary of properties you want added to the\n PickEvent attributes.\n\nAfter you have enabled an artist for picking by setting the \"picker\"\nproperty, you need to connect to the figure canvas pick_event to get\npick callbacks on mouse press events. For example, ::\n\n def pick_handler(event):\n mouseevent = event.mouseevent\n artist = event.artist\n # now do something with this...\n\n\nThe pick event (matplotlib.backend_bases.PickEvent) which is passed to\nyour callback is always fired with two attributes:\n\nmouseevent\n the mouse event that generate the pick event.\n\n The mouse event in turn has attributes like x and y (the coordinates in\n display space, such as pixels from left, bottom) and xdata, ydata (the\n coords in data space). Additionally, you can get information about\n which buttons were pressed, which keys were pressed, which Axes\n the mouse is over, etc. See matplotlib.backend_bases.MouseEvent\n for details.\n\nartist\n the matplotlib.artist that generated the pick event.\n\nAdditionally, certain artists like Line2D and PatchCollection may\nattach additional metadata like the indices into the data that meet\nthe picker criteria (for example, all the points in the line that are within\nthe specified epsilon tolerance)\n\nThe examples below illustrate each of these methods.\n\n
These examples exercises the interactive capabilities of Matplotlib, and\n this will not appear in the static documentation. Please run this code on\n your machine to see the interactivity.\n\n You can copy and paste individual parts, or download the entire example\n using the link at the bottom of the page.