widgets.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """
  2. ========================
  3. Widget testing utilities
  4. ========================
  5. Functions that are useful for testing widgets.
  6. See also matplotlib.tests.test_widgets
  7. """
  8. import matplotlib.pyplot as plt
  9. from unittest import mock
  10. def get_ax():
  11. """Creates plot and returns its axes"""
  12. fig, ax = plt.subplots(1, 1)
  13. ax.plot([0, 200], [0, 200])
  14. ax.set_aspect(1.0)
  15. ax.figure.canvas.draw()
  16. return ax
  17. def do_event(tool, etype, button=1, xdata=0, ydata=0, key=None, step=1):
  18. """
  19. Trigger an event
  20. Parameters
  21. ----------
  22. tool : matplotlib.widgets.RectangleSelector
  23. etype
  24. the event to trigger
  25. xdata : int
  26. x coord of mouse in data coords
  27. ydata : int
  28. y coord of mouse in data coords
  29. button : int or str
  30. button pressed None, 1, 2, 3, 'up', 'down' (up and down are used
  31. for scroll events)
  32. key
  33. the key depressed when the mouse event triggered (see
  34. :class:`KeyEvent`)
  35. step : int
  36. number of scroll steps (positive for 'up', negative for 'down')
  37. """
  38. event = mock.Mock()
  39. event.button = button
  40. ax = tool.ax
  41. event.x, event.y = ax.transData.transform([(xdata, ydata),
  42. (xdata, ydata)])[0]
  43. event.xdata, event.ydata = xdata, ydata
  44. event.inaxes = ax
  45. event.canvas = ax.figure.canvas
  46. event.key = key
  47. event.step = step
  48. event.guiEvent = None
  49. event.name = 'Custom'
  50. func = getattr(tool, etype)
  51. func(event)