选择事件演示

您可以通过设置艺术家的“选择器”属性来启用拾取(例如,matplotlib Line2D,Text,Patch,Polygon,AxesImage等…)

选择器属性有多种含义

  • None - 此艺术家对象的选择功能已停用(默认)
  • boolean - 如果为True,则启用拾取,如果鼠标事件在艺术家上方,艺术家将触发拾取事件
  • float - 如果选择器是一个数字,则它被解释为以点为单位的epsilon容差,如果事件的数据在鼠标事件的epsilon内,则艺术家将触发事件。 对于某些艺术家(如线条和补丁集合),艺术家可能会为生成的挑选事件提供其他数据,例如,挑选事件的epsilon中的数据索引
  • function - 如果选择器是可调用的,则它是用户提供的函数,用于确定艺术家是否被鼠标事件命中。

    hit, props = picker(artist, mouseevent)

    确定命中测试。 如果鼠标事件在艺术家上方,则返回hit = True,props是要添加到PickEvent属性的属性字典

通过设置“选取器”属性启用艺术家进行拾取后,您需要连接到图形画布pick_event以获取鼠标按下事件的拾取回调。 例如,

  1. def pick_handler(event):
  2. mouseevent = event.mouseevent artist = event.artist # now do something with this...

传递给回调的pick事件(matplotlib.backend_bases.PickEvent)始终使用两个属性触发:

  • mouseevent - 生成拾取事件的鼠标事件。 鼠标事件又具有x和y(显示空间中的坐标,如左下角的像素)和xdata,ydata(数据空间中的坐标)等属性。 此外,您可以获取有关按下哪些按钮,按下哪些键,鼠标所在的轴等的信息。有关详细信息,请参阅matplotlib.backend_bases.MouseEvent。

  • artist - 生成pick事件的matplotlib.artist。

此外,某些艺术家(如Line2D和PatchCollection)可能会将其他元数据(如索引)附加到符合选择器条件的数据中(例如,行中指定的epsilon容差范围内的所有点)

以下示例说明了这些方法中的每一种。

选择事件示例

选择事件示例2

选择事件示例3

选择事件示例4

  1. import matplotlib.pyplot as plt
  2. from matplotlib.lines import Line2D
  3. from matplotlib.patches import Rectangle
  4. from matplotlib.text import Text
  5. from matplotlib.image import AxesImage
  6. import numpy as np
  7. from numpy.random import rand
  8. if 1: # simple picking, lines, rectangles and text
  9. fig, (ax1, ax2) = plt.subplots(2, 1)
  10. ax1.set_title('click on points, rectangles or text', picker=True)
  11. ax1.set_ylabel('ylabel', picker=True, bbox=dict(facecolor='red'))
  12. line, = ax1.plot(rand(100), 'o', picker=5) # 5 points tolerance
  13. # pick the rectangle
  14. bars = ax2.bar(range(10), rand(10), picker=True)
  15. for label in ax2.get_xticklabels(): # make the xtick labels pickable
  16. label.set_picker(True)
  17. def onpick1(event):
  18. if isinstance(event.artist, Line2D):
  19. thisline = event.artist
  20. xdata = thisline.get_xdata()
  21. ydata = thisline.get_ydata()
  22. ind = event.ind
  23. print('onpick1 line:', zip(np.take(xdata, ind), np.take(ydata, ind)))
  24. elif isinstance(event.artist, Rectangle):
  25. patch = event.artist
  26. print('onpick1 patch:', patch.get_path())
  27. elif isinstance(event.artist, Text):
  28. text = event.artist
  29. print('onpick1 text:', text.get_text())
  30. fig.canvas.mpl_connect('pick_event', onpick1)
  31. if 1: # picking with a custom hit test function
  32. # you can define custom pickers by setting picker to a callable
  33. # function. The function has the signature
  34. #
  35. # hit, props = func(artist, mouseevent)
  36. #
  37. # to determine the hit test. if the mouse event is over the artist,
  38. # return hit=True and props is a dictionary of
  39. # properties you want added to the PickEvent attributes
  40. def line_picker(line, mouseevent):
  41. """
  42. find the points within a certain distance from the mouseclick in
  43. data coords and attach some extra attributes, pickx and picky
  44. which are the data points that were picked
  45. """
  46. if mouseevent.xdata is None:
  47. return False, dict()
  48. xdata = line.get_xdata()
  49. ydata = line.get_ydata()
  50. maxd = 0.05
  51. d = np.sqrt((xdata - mouseevent.xdata)**2. + (ydata - mouseevent.ydata)**2.)
  52. ind = np.nonzero(np.less_equal(d, maxd))
  53. if len(ind):
  54. pickx = np.take(xdata, ind)
  55. picky = np.take(ydata, ind)
  56. props = dict(ind=ind, pickx=pickx, picky=picky)
  57. return True, props
  58. else:
  59. return False, dict()
  60. def onpick2(event):
  61. print('onpick2 line:', event.pickx, event.picky)
  62. fig, ax = plt.subplots()
  63. ax.set_title('custom picker for line data')
  64. line, = ax.plot(rand(100), rand(100), 'o', picker=line_picker)
  65. fig.canvas.mpl_connect('pick_event', onpick2)
  66. if 1: # picking on a scatter plot (matplotlib.collections.RegularPolyCollection)
  67. x, y, c, s = rand(4, 100)
  68. def onpick3(event):
  69. ind = event.ind
  70. print('onpick3 scatter:', ind, np.take(x, ind), np.take(y, ind))
  71. fig, ax = plt.subplots()
  72. col = ax.scatter(x, y, 100*s, c, picker=True)
  73. #fig.savefig('pscoll.eps')
  74. fig.canvas.mpl_connect('pick_event', onpick3)
  75. if 1: # picking images (matplotlib.image.AxesImage)
  76. fig, ax = plt.subplots()
  77. im1 = ax.imshow(rand(10, 5), extent=(1, 2, 1, 2), picker=True)
  78. im2 = ax.imshow(rand(5, 10), extent=(3, 4, 1, 2), picker=True)
  79. im3 = ax.imshow(rand(20, 25), extent=(1, 2, 3, 4), picker=True)
  80. im4 = ax.imshow(rand(30, 12), extent=(3, 4, 3, 4), picker=True)
  81. ax.axis([0, 5, 0, 5])
  82. def onpick4(event):
  83. artist = event.artist
  84. if isinstance(artist, AxesImage):
  85. im = artist
  86. A = im.get_array()
  87. print('onpick4 image', A.shape)
  88. fig.canvas.mpl_connect('pick_event', onpick4)
  89. plt.show()

下载这个示例