|
| 1 | +from __future__ import (absolute_import, division, print_function, |
| 2 | + unicode_literals) |
| 3 | + |
| 4 | +try: |
| 5 | + # mock in python 3.3+ |
| 6 | + from unittest import mock |
| 7 | +except ImportError: |
| 8 | + import mock |
| 9 | + |
| 10 | +import matplotlib.widgets as widgets |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +from matplotlib.testing.decorators import cleanup |
| 13 | + |
| 14 | + |
| 15 | +def get_event(ax, button=1, xdata=0, ydata=0, key=None, step=1): |
| 16 | + """ |
| 17 | + *name* |
| 18 | + the event name |
| 19 | +
|
| 20 | + *canvas* |
| 21 | + the FigureCanvas instance generating the event |
| 22 | +
|
| 23 | + *guiEvent* |
| 24 | + the GUI event that triggered the matplotlib event |
| 25 | +
|
| 26 | + *x* |
| 27 | + x position - pixels from left of canvas |
| 28 | +
|
| 29 | + *y* |
| 30 | + y position - pixels from bottom of canvas |
| 31 | +
|
| 32 | + *inaxes* |
| 33 | + the :class:`~matplotlib.axes.Axes` instance if mouse is over axes |
| 34 | +
|
| 35 | + *xdata* |
| 36 | + x coord of mouse in data coords |
| 37 | +
|
| 38 | + *ydata* |
| 39 | + y coord of mouse in data coords |
| 40 | +
|
| 41 | + *button* |
| 42 | + button pressed None, 1, 2, 3, 'up', 'down' (up and down are used |
| 43 | + for scroll events) |
| 44 | +
|
| 45 | + *key* |
| 46 | + the key depressed when the mouse event triggered (see |
| 47 | + :class:`KeyEvent`) |
| 48 | +
|
| 49 | + *step* |
| 50 | + number of scroll steps (positive for 'up', negative for 'down') |
| 51 | + """ |
| 52 | + event = mock.Mock() |
| 53 | + event.button = button |
| 54 | + event.x, event.y = ax.transData.transform([(xdata, ydata), |
| 55 | + (xdata, ydata)])[00] |
| 56 | + event.xdata, event.ydata = xdata, ydata |
| 57 | + event.inaxes = ax |
| 58 | + event.canvas = ax.figure.canvas |
| 59 | + event.key = key |
| 60 | + event.step = step |
| 61 | + event.guiEvent = None |
| 62 | + event.name = 'Custom' |
| 63 | + return event |
| 64 | + |
| 65 | + |
| 66 | +@cleanup |
| 67 | +def check_rectangle(**kwargs): |
| 68 | + fig, ax = plt.subplots(1, 1) |
| 69 | + ax.plot([0, 200], [0, 200]) |
| 70 | + ax.figure.canvas.draw() |
| 71 | + |
| 72 | + def onselect(epress, erelease): |
| 73 | + ax._got_onselect = True |
| 74 | + assert epress.xdata == 100 |
| 75 | + assert epress.ydata == 100 |
| 76 | + assert erelease.xdata == 200 |
| 77 | + assert erelease.ydata == 200 |
| 78 | + |
| 79 | + tool = widgets.RectangleSelector(ax, onselect, **kwargs) |
| 80 | + event = get_event(ax, xdata=100, ydata=100, button=1) |
| 81 | + tool.press(event) |
| 82 | + |
| 83 | + event = get_event(ax, xdata=125, ydata=125, button=1) |
| 84 | + tool.onmove(event) |
| 85 | + |
| 86 | + # purposely drag outside of axis for release |
| 87 | + event = get_event(ax, xdata=250, ydata=250, button=1) |
| 88 | + tool.release(event) |
| 89 | + |
| 90 | + assert ax._got_onselect |
| 91 | + |
| 92 | + |
| 93 | +def test_rectangle_selector(): |
| 94 | + check_rectangle() |
| 95 | + check_rectangle(drawtype='line', useblit=False) |
| 96 | + check_rectangle(useblit=True, button=1) |
| 97 | + check_rectangle(drawtype='none', minspanx=10, minspany=10) |
| 98 | + check_rectangle(minspanx=10, minspany=10, spancoords='pixels') |
| 99 | + check_rectangle(rectprops=dict(fill=True)) |
| 100 | + |
| 101 | + |
| 102 | +@cleanup |
| 103 | +def check_span(*args, **kwargs): |
| 104 | + fig, ax = plt.subplots(1, 1) |
| 105 | + ax.plot([0, 200], [0, 200]) |
| 106 | + ax.figure.canvas.draw() |
| 107 | + |
| 108 | + def onselect(vmin, vmax): |
| 109 | + ax._got_onselect = True |
| 110 | + assert vmin == 100 |
| 111 | + assert vmax == 150 |
| 112 | + |
| 113 | + def onmove(vmin, vmax): |
| 114 | + assert vmin == 100 |
| 115 | + assert vmax == 125 |
| 116 | + ax._got_on_move = True |
| 117 | + |
| 118 | + if 'onmove_callback' in kwargs: |
| 119 | + kwargs['onmove_callback'] = onmove |
| 120 | + |
| 121 | + tool = widgets.SpanSelector(ax, onselect, *args, **kwargs) |
| 122 | + event = get_event(ax, xdata=100, ydata=100, button=1) |
| 123 | + tool.press(event) |
| 124 | + |
| 125 | + event = get_event(ax, xdata=125, ydata=125, button=1) |
| 126 | + tool.onmove(event) |
| 127 | + |
| 128 | + event = get_event(ax, xdata=150, ydata=150, button=1) |
| 129 | + tool.release(event) |
| 130 | + |
| 131 | + assert ax._got_onselect |
| 132 | + |
| 133 | + if 'onmove_callback' in kwargs: |
| 134 | + assert ax._got_on_move |
| 135 | + |
| 136 | + |
| 137 | +def test_span_selector(): |
| 138 | + check_span('horizontal', minspan=10, useblit=True) |
| 139 | + check_span('vertical', onmove_callback=True, button=1) |
| 140 | + check_span('horizontal', rectprops=dict(fill=True)) |
| 141 | + |
| 142 | + |
| 143 | +@cleanup |
| 144 | +def check_lasso_selector(**kwargs): |
| 145 | + fig, ax = plt.subplots(1, 1) |
| 146 | + ax = plt.gca() |
| 147 | + ax.plot([0, 200], [0, 200]) |
| 148 | + ax.figure.canvas.draw() |
| 149 | + |
| 150 | + def onselect(verts): |
| 151 | + ax._got_onselect = True |
| 152 | + assert verts == [(100, 100), (125, 125), (150, 150)] |
| 153 | + |
| 154 | + tool = widgets.LassoSelector(ax, onselect, **kwargs) |
| 155 | + event = get_event(ax, xdata=100, ydata=100, button=1) |
| 156 | + tool.press(event) |
| 157 | + |
| 158 | + event = get_event(ax, xdata=125, ydata=125, button=1) |
| 159 | + tool.onmove(event) |
| 160 | + |
| 161 | + event = get_event(ax, xdata=150, ydata=150, button=1) |
| 162 | + tool.release(event) |
| 163 | + |
| 164 | + assert ax._got_onselect |
| 165 | + |
| 166 | + |
| 167 | +def test_lasso_selector(): |
| 168 | + check_lasso_selector() |
| 169 | + check_lasso_selector(useblit=False, lineprops=dict(color='red')) |
| 170 | + check_lasso_selector(useblit=True, button=1) |
0 commit comments