综合编辑器

这是一个示例,展示如何使用Matplotlib事件处理来构建跨GUI应用程序,以与画布上的对象进行交互。

综合编辑器示例

  1. import numpy as np
  2. from matplotlib.lines import Line2D
  3. from matplotlib.artist import Artist
  4. from matplotlib.mlab import dist_point_to_segment
  5. class PolygonInteractor(object):
  6. """
  7. A polygon editor.
  8. Key-bindings
  9. 't' toggle vertex markers on and off. When vertex markers are on,
  10. you can move them, delete them
  11. 'd' delete the vertex under point
  12. 'i' insert a vertex at point. You must be within epsilon of the
  13. line connecting two existing vertices
  14. """
  15. showverts = True
  16. epsilon = 5 # max pixel distance to count as a vertex hit
  17. def __init__(self, ax, poly):
  18. if poly.figure is None:
  19. raise RuntimeError('You must first add the polygon to a figure '
  20. 'or canvas before defining the interactor')
  21. self.ax = ax
  22. canvas = poly.figure.canvas
  23. self.poly = poly
  24. x, y = zip(*self.poly.xy)
  25. self.line = Line2D(x, y,
  26. marker='o', markerfacecolor='r',
  27. animated=True)
  28. self.ax.add_line(self.line)
  29. self.cid = self.poly.add_callback(self.poly_changed)
  30. self._ind = None # the active vert
  31. canvas.mpl_connect('draw_event', self.draw_callback)
  32. canvas.mpl_connect('button_press_event', self.button_press_callback)
  33. canvas.mpl_connect('key_press_event', self.key_press_callback)
  34. canvas.mpl_connect('button_release_event', self.button_release_callback)
  35. canvas.mpl_connect('motion_notify_event', self.motion_notify_callback)
  36. self.canvas = canvas
  37. def draw_callback(self, event):
  38. self.background = self.canvas.copy_from_bbox(self.ax.bbox)
  39. self.ax.draw_artist(self.poly)
  40. self.ax.draw_artist(self.line)
  41. # do not need to blit here, this will fire before the screen is
  42. # updated
  43. def poly_changed(self, poly):
  44. 'this method is called whenever the polygon object is called'
  45. # only copy the artist props to the line (except visibility)
  46. vis = self.line.get_visible()
  47. Artist.update_from(self.line, poly)
  48. self.line.set_visible(vis) # don't use the poly visibility state
  49. def get_ind_under_point(self, event):
  50. 'get the index of the vertex under point if within epsilon tolerance'
  51. # display coords
  52. xy = np.asarray(self.poly.xy)
  53. xyt = self.poly.get_transform().transform(xy)
  54. xt, yt = xyt[:, 0], xyt[:, 1]
  55. d = np.hypot(xt - event.x, yt - event.y)
  56. indseq, = np.nonzero(d == d.min())
  57. ind = indseq[0]
  58. if d[ind] >= self.epsilon:
  59. ind = None
  60. return ind
  61. def button_press_callback(self, event):
  62. 'whenever a mouse button is pressed'
  63. if not self.showverts:
  64. return
  65. if event.inaxes is None:
  66. return
  67. if event.button != 1:
  68. return
  69. self._ind = self.get_ind_under_point(event)
  70. def button_release_callback(self, event):
  71. 'whenever a mouse button is released'
  72. if not self.showverts:
  73. return
  74. if event.button != 1:
  75. return
  76. self._ind = None
  77. def key_press_callback(self, event):
  78. 'whenever a key is pressed'
  79. if not event.inaxes:
  80. return
  81. if event.key == 't':
  82. self.showverts = not self.showverts
  83. self.line.set_visible(self.showverts)
  84. if not self.showverts:
  85. self._ind = None
  86. elif event.key == 'd':
  87. ind = self.get_ind_under_point(event)
  88. if ind is not None:
  89. self.poly.xy = np.delete(self.poly.xy,
  90. ind, axis=0)
  91. self.line.set_data(zip(*self.poly.xy))
  92. elif event.key == 'i':
  93. xys = self.poly.get_transform().transform(self.poly.xy)
  94. p = event.x, event.y # display coords
  95. for i in range(len(xys) - 1):
  96. s0 = xys[i]
  97. s1 = xys[i + 1]
  98. d = dist_point_to_segment(p, s0, s1)
  99. if d <= self.epsilon:
  100. self.poly.xy = np.insert(
  101. self.poly.xy, i+1,
  102. [event.xdata, event.ydata],
  103. axis=0)
  104. self.line.set_data(zip(*self.poly.xy))
  105. break
  106. if self.line.stale:
  107. self.canvas.draw_idle()
  108. def motion_notify_callback(self, event):
  109. 'on mouse movement'
  110. if not self.showverts:
  111. return
  112. if self._ind is None:
  113. return
  114. if event.inaxes is None:
  115. return
  116. if event.button != 1:
  117. return
  118. x, y = event.xdata, event.ydata
  119. self.poly.xy[self._ind] = x, y
  120. if self._ind == 0:
  121. self.poly.xy[-1] = x, y
  122. elif self._ind == len(self.poly.xy) - 1:
  123. self.poly.xy[0] = x, y
  124. self.line.set_data(zip(*self.poly.xy))
  125. self.canvas.restore_region(self.background)
  126. self.ax.draw_artist(self.poly)
  127. self.ax.draw_artist(self.line)
  128. self.canvas.blit(self.ax.bbox)
  129. if __name__ == '__main__':
  130. import matplotlib.pyplot as plt
  131. from matplotlib.patches import Polygon
  132. theta = np.arange(0, 2*np.pi, 0.1)
  133. r = 1.5
  134. xs = r * np.cos(theta)
  135. ys = r * np.sin(theta)
  136. poly = Polygon(np.column_stack([xs, ys]), animated=True)
  137. fig, ax = plt.subplots()
  138. ax.add_patch(poly)
  139. p = PolygonInteractor(ax, poly)
  140. ax.set_title('Click and drag a point to move it')
  141. ax.set_xlim((-2, 2))
  142. ax.set_ylim((-2, 2))
  143. plt.show()

下载这个示例